1

We have a BrilliantRetail cart and I'm adding JSON data into an attribute named itemJSON. In the cart template, we loop through the items in the cart using the {items}{/items} tag and I need to show certain values from the JSON stored in the attribute.

Using the

{itemJSON}

tag shows the full JSON. I only want some of the values stored there without having to use PHP in my template. Is this doable? Doing it like either of these fails, it shows the tag, instead:

{itemJSON->SP->FN}
{itemJSON.SP.FN}

Would there be a way to use preload_replace to do this?

Edited->showing code: This doesn't seem to be working correctly:

$json = json_decode($this->EE->TMPL->fetch_param("json"), true);
$values["StudentName"]    = $json["ShortParticipants"][0]["FirstName"];
$values["CEState"]        = $json["ShortParticipants"][0]["CEInfo"]["CEState"];
$values["CENameOnLic"]    = $json["ShortParticipants"][0]["CEInfo"]["CEFirstName"]." ".$json["ShortParticipants"][0]["CEInfo"]["CELastName"];
$values["CELicencelabel"] = $json["ShortParticipants"][0]["CEInfo"]["CELicenseText"];
$values["CELicenseNum"]   = $json["ShortParticipants"][0]["CEInfo"]["CELicense"];
$values["CEHours"]        = $json["ShortParticipants"][0]["CEInfo"]["Hours"];
$values["course_text"]    = file_get_contents("http://".$_SERVER["HTTP_HOST"]."/files/getCourseText.php?EventCode=".$json["eventcode"]."&Cart=yes");

$vars = array((array) $values);

$this->return_data = $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $vars);        

Also, in using the tag like you show:

{exp:tnajson json="{itemJSON}"}
    {if sku == "CE_FEE"}
    <i>CE For Student: {StudentName}</i><br />
    Course: {course_text}<br />
    <hr style="color: #D9DDE3; background-color: #D9DDE3; margin-bottom: 5px;" />
    CE State: {CEState}<br />
    Name On License: {CENameOnLic}<br />
    {CELicencelabel}: {CELicenseNum}<br />
    Hours: {CEHours}<br />  
    {if:else}
    <i>For Student: {StudentName}</i><br />
    {course_text}
    {/if}
{/exp:tnajson}

It shows this:

}}"}

And then none of the values.

2

Not sure what existing solution there are, but I would just create a real simple plugin with pkg.io.

Basically, your constructor method would look like this:

public function constructor()
{
    $json = json_encode($this->EE->TMPL->fetch_param('json'));

    // Now you need to parse the JSON object. This will be unique
    // depending on what your JSON object looks like.
    // This example assumes you have an object 1 level deep and it needs
    // converted to an indexed associated array.

    $vars = array((array) $json);

    $this->return_data = $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $vars);
}

Your template tags will look like this:

{exp:your_plugin json="{itemJSON}"}
    {your_obj_property_name_1}
    {your_obj_property_name_2}
    {your_obj_property_name_3}
{/exp:your_plugin}
11
  • Explain this: $vars = array((array) $json); – MB34 Jul 18 '13 at 16:44
  • That just converts and object into an associated array, and then inserts it into another array. This is known as an "indexed associative array". So you end up with an array of associative arrays. Alternatively, you can use $this->EE->TMPL->parse_variables_row() which just accepts an associative array (without an indexed array). ellislab.com/expressionengine/user-guide/development/usage/… – Justin Kimbrell Jul 18 '13 at 16:52
  • It's not working as I expected, see edits to original. – MB34 Jul 18 '13 at 17:08
  • TBH, I can't say for why sure what it's not parsing. It looks correct as far as I can tell. Add var_dump($vars);exit(); right before you send it to parser. See if your values are correct. Another place to check would be to ensure the $json variable has the expected values. It seems to be correct, or I would think there would be PHP errors denoting that. – Justin Kimbrell Jul 18 '13 at 17:24
  • I think it may have to do with the way the {itemJSON} is being sent to the plugin. – MB34 Jul 18 '13 at 17:28

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.