0

EE 5.2.4

I'm developing a feature for custom analytics. Certain activities are saved as channel entries in order to be presented in a dashboard later.

I have the first part of the feature working. The way I've gone about it is to save the activity as a string of JSON in a channel entry field. My intention was to parse this out later, as I'm not sure what sort of technology I'll be using on the dashboard and JSON seemed like a universal/consistent format to store the data.

My issue is when I try to pass the JSON string as a tag parameter into a php class method, it doesn't like the JSON and returns nothing.

Example.

Template:

{exp:channel:entries channel='analytics' dynamic='no' status='not closed'}
    {exp:class:my_method json='{json}'}
{/exp:channel:entries}

Class:

  function my_method() {

    $json = ee()->TMPL->fetch_param('json');
    print_r($json);
  }

When the {json} is the saved JSON string from the channel (I know that's working because it shows just fine in the template), the $json variable will return nothing, but if I replace {json} in json='{json}' with a normal string or even another entry field like {title} it will work just fine and I'll get a correct print.

Any ideas what's happening to this JSON string?

I could use php to work with this data in the template, for example

<? php 
   $json = '{json}';
   print_r(json_decode($json, true));
?>

but I'd really rather use the EE template features if possible because I just like them so much.

Thanks for your help!

1

Tags in params aren't going to be parsed unless you add this parameter:

parse="inward"

The parse order stuff is... hard to grok. Try adding that to your channel entry tag.

https://docs.expressionengine.com/latest/templates/language.html#changing-parsing-order

| improve this answer | |
  • Thanks for the answer! I'm not sure I understand, though. I added parse='inward' to my channel entry tag and the result changed from nothing to '}. In the documentation you linked it uses an rss_parser tag? {exp:rss_parser url="https://ellislab.com/blog/rss-feed" limit="5" parse="inward"} – Shayne Aug 22 '19 at 15:29
  • Hrmm. The issue there might be that your JSON string actually has single quotes in it, resulting in a parameter that ends up looking like this: json='{'key': 'value'}' which would confuse the parser. Try putting a plain string like test into your JSON field and see if it gets passed correctly. – jrothafer Aug 22 '19 at 17:13
  • And the doc example is just that; and example. Ignore the rss_parse tag, it could be any EE method tag. – jrothafer Aug 22 '19 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.