继this question ,我现在正在尝试重新设计插件,以便我可以做到:
{exp:deetector}
{user_agent}
{hash}
{/exp:deetector}
但是使用下面的代码,我没有得到任何输出:
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 数组则不会。我也试过
$variables = array
但得到 undefined offset :0。
最佳答案
如果您只是使用构造函数进行输出,请确保插件类具有公共(public)属性 return_data
其中包含解析的标记数据:
$this->return_data = $this->EE->TMPL->parse_variables($tagdata, $variables);
对于类中的任何其他方法,您可以根据您的示例简单地返回解析的数据。
作为旁注,我认为您没有在此处循环任何数据。考虑使用
parse_variables_row
方法,所以额外的变量,如 count
, total_results
和 switch
被省略。使用该方法不需要嵌套数组,因此可以归结为:$variables = array(
'user_agent' => $this->ua->ua,
...
);
$this->return_data = $this->EE->TMPL->parse_variables_row($tagdata, $variables);
关于expressionengine - 在插件中输出变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13114460/