Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outputting variables within a plugin

Following on from this question, I'm now trying to rework the plugin so that I can do:

{exp:deetector}
     {user_agent}
     {hash}
{/exp:deetector}

but with the code below, I get no output:

public function __construct()
{
    $this->EE =& get_instance();

    include(PATH_THIRD.'/deetector/libraries/detector.php');

    $this->ua = $ua;

    $tagdata = $this->EE->TMPL->tagdata;

    $variables[] = array(
        'user_agent'   => $this->ua->ua,
        'hash'         => $this->ua->uaHash,
        'browser_os'   => $this->ua->full,
        'browser'      => $this->ua->browser,
        'browser_full' => $this->ua->browserFull
    );

    return $this->EE->TMPL->parse_variables($tagdata, $variables);
}

If I do $this->return_data = $this->ua->xx for each of the variables listed above I get output, but not if I parse the $variables array.

I've also tried $variables = array but get Undefined offset: 0.

like image 607
Tyssen Avatar asked Dec 15 '22 18:12

Tyssen


1 Answers

If you're just using the constructor for output, make sure the plugin class has a public property return_data which contains the parsed tagdata:

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

For any other method in the class, you can just simply return the parsed data, as per your example.

As a sidenote, I take it you're not looping any data here. Consider using the parse_variables_row method instead, so extra variables like count, total_results and switch are omitted. Using that method doesn't require a nested array, so it would come down to this:

$variables = array(
    'user_agent' => $this->ua->ua,
    ...
);

$this->return_data = $this->EE->TMPL->parse_variables_row($tagdata, $variables);
like image 160
Low Avatar answered Jan 20 '23 11:01

Low