在插件中输出变量

Tys*_*sen 4 expressionengine

这个问题之后,我现在正在尝试重新编写插件,以便我可以这样做:

{exp:deetector}
     {user_agent}
     {hash}
{/exp:deetector}
Run Code Online (Sandbox Code Playgroud)

但是使用下面的代码,我没有输出:

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);
}
Run Code Online (Sandbox Code Playgroud)

如果我$this->return_data = $this->ua->xx对上面列出的每个变量都这样做,我得到输出,但是如果我解析$ variables数组则不行.

我也试过$variables = array但得到Undefined offset:0.

Low*_*Low 10

如果您只是使用构造函数进行输出,请确保插件类具有return_data包含已解析标记数据的公共属性:

$this->return_data = $this->EE->TMPL->parse_variables($tagdata, $variables);
Run Code Online (Sandbox Code Playgroud)

对于类中的任何其他方法,您只需返回解析后的数据,根据您的示例.

作为旁注,我认为你不是在这里循环任何数据.请考虑使用parse_variables_row替代方法,所以额外的变量,如count,total_resultsswitch省略.使用该方法不需要嵌套数组,因此它将归结为:

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

$this->return_data = $this->EE->TMPL->parse_variables_row($tagdata, $variables);
Run Code Online (Sandbox Code Playgroud)