4

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.

Community
  • 1
  • 1
Tyssen
  • 1,569
  • 16
  • 35

2 Answers2

10

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);
Low
  • 566
  • 5
  • 11
  • could you elaborate a bit more on _make sure the plugin class has a public property return_data which contains the parsed tagdata_ ? Because if I have `public $return_data = $something_else` I get _unexpected T_VARIABLE_. – Tyssen Oct 29 '12 at 12:26
  • It should be defined as a class property, not necessarily in the method itself. Just leaving it as `$this->return_data = 'whatever';` will work fine as well. – Low Oct 29 '12 at 13:17
  • Thanks, making it `$this->return_data = $this->EE->TMPL->parse_variables_row($tagdata, $variables);` has done the trick. :) – Tyssen Oct 29 '12 at 22:38
  • @Low - Will using `parse_variables_row()` prevent the variables from being used in conditionals? e.g. `{if user_agent = "blah"}` – Adrian Macneil Oct 30 '12 at 04:06
2

Regarding the other post you referenced, nobody pointed out that you had defined 2 constructor functions:

__construct() and deetector()

You should drop the second one and just use __construct(). Not sure if that's possibly causing the strange PHP errors.

RAS
  • 8,100
  • 16
  • 64
  • 86
John D Wells
  • 198
  • 5
  • I've actually removed that function in this version. – Tyssen Oct 29 '12 at 22:33
  • That was pointed out in my answer on the other question. Don't think it was causing this particular problem, but it could definitely cause unexpected behaviour. – Adrian Macneil Oct 30 '12 at 04:04
  • You could use both, as long as only this is in the `deetector()` method: `return $this->__construct();` – Low Oct 30 '12 at 11:24