在插件中输出变量 [英] 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);
}
如果我对上面列出的每个变量执行$this->return_data = $this->ua->xx
,我将得到输出,但如果我解析$ 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.
我也尝试过$variables = array
,但得到未定义偏移量:0 .
推荐答案
如果仅使用构造函数进行输出,请确保插件类具有公共属性return_data
,其中包含解析的tagdata:
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.
作为旁注,我认为您不在此处循环任何数据.考虑改用parse_variables_row
方法,因此省略了count
,total_results
和switch
之类的多余变量.使用该方法不需要嵌套数组,因此可以归结为:
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);
这篇关于在插件中输出变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!