0

I made my first plugin I don't deal allot with variable and parsing. it was a JSON data to pars to a template. the problem is when I decode the JSON to an array Then I use parse the variables to the template I can't get the variables in my plugin tag. I found a question similar to my question but it did work for me either .

Parsing JSON in EE tag

can i get help please :D

    public function __construct()
{
    $this->EE =& get_instance();
    $find   = array("<", ">");
    $rep = array("{", "}");
    $json = str_replace($find, $rep, $this->EE->TMPL->fetch_param('json'));
    $json = current(json_decode($json,true));
    $json['location'] = array(
        array('lat'=>$json['location']['lat'],'lng'=>$json['location']['lng'])
    );
    $json['social'] = array(
        array('twitter'=>$json['social']['twitter'],'facebook'=>$json['social']['facebook'],'LinkedIn'=>$json['social']['LinkedIn'])
    );


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

    $this->return_data = $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $vars);
}
2
  • Can you post the relevant portion of your code, and an example of what the resulting array looks like? Commented Apr 14, 2015 at 12:58
  • sure and thanks :) I will update the question it self
    – Akwad Net
    Commented Apr 14, 2015 at 13:11

2 Answers 2

1

If it's not coming through, I'd bet it's malformed JSON, or more likely, your array structure is incorrect.

https://ellislab.com/expressionengine/user-guide/development/usage/template.html#parsing-variables

An easy way to test this is to dump your final array to the developer logger as json, and just compare the structure to how it should be as an array. To do this, add these lines of code right before you call parse_variables:

ee()->load->library('logger');
ee()->logger->developer(json_encode($vars));

Then check the log in your control panel. It lives under Tools -> Logs -> Developer Log. Almost certainly you just have a malformed array structure. Compare your structure to the Ellis Lab examples.

0

in my case i get some json from other website it gives an ID for the returned json value. so I only needed the first element when decoding so I used $json = current(json_decode($json,true)) and then I add this element of array the $vars = (array) array($json); after that I pared the variable as well.

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

}

now it works find with me . thanks allot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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