ExpressionEngine模板:将插件/模块的输出作为参数传递给另一个插件/模块

时间:2010-12-02 01:12:38

标签: templates expressionengine

这基本上就是我想要完成的事情:

{exp:plugin1:method arg="{exp:plugin2:method}"}

我尝试了很多不同的方法。

方法1:

{exp:plugin1:method arg="{exp:plugin2:method}"}

结果: Plugin1->method的{​​{1}}参数值是字符串arg,它永远不会被解析。

方法2:

我对解析顺序的理解表明,这可能会有不同的结果,但显然它没有。

{exp:plugin2:method}

结果{preload_replace:replaced="{exp:plugin2:method}"} {exp:plugin1:method arg="{replaced}"} 参数与方法1的值相同。

方法3:

首先我定义一个代码段(arg),其内容为:

snip

然后在模板中:

{exp:plugin2:method}

结果:与方法1和2相同。

方法4:

注意到插件按照它们出现的顺序进行处理,我甚至在{exp:plugin1:method arg="{snip}"} 调用之前只测试了{exp:plugin2:method}的实例。我的想法是,我可以在正则表达式替换插件中包装第一个调用以抑制输出,但它会首先触发Plugin2的解析。

{exp:plugin1:method}

结果: {exp:plugin2:method} {exp:plugin1:method arg="{exp:plugin2:method}"} 的{​​{1}}参数值是Plugin1->method输出的临时哈希占位符(我相信MD5),模板类保留直到晚些时候。

2 个答案:

答案 0 :(得分:2)

有趣的方法。但是,这可以更简单地实现:

{exp:plugin1:method arg="{exp:plugin2:method}" parse="inward"}

答案 1 :(得分:0)

我有一个解决方法,但我会等待一段时间,看看在接受我自己的答案之前是否有更好的解决方案。解决方法是使用plugin1包裹plugin2并替换tagdata中引用其方法的模板标记。请注意,这需要在parse="inward"电话上plugin2参数。

在模板中:

{exp:plugin2 parse="inward"}
    {exp:plugin1:method arg="{someplugin2method}"}
{/exp:plugin2}

在插件类中:

static $public_methods;

function __construct() {
    // Actual construction code omitted...

    if(($tagdata = $this->EE->TMPL->tagdata) !== false && trim($tagdata) !== '') {
        if(!isset(self::$public_methods)) {
            self::$public_methods = array();
            $methods = get_class_methods($this);
            foreach($methods as $method) {
                if($method == get_class($this) || $method == '__construct') {
                    continue;
                }
                $reflection = new ReflectionMethod(get_class($this), $method);
                if($reflection->isPublic()) {
                    self::$public_methods[] = $method;
                }
            }

            self::$public_methods = implode('|', self::$public_methods);
        }

        $tagdata = preg_replace_callback('/\{(' . self::$public_methods . ')\}/',
            array($this, 'tagdata_callback'), $tagdata);
        $this->return_data = $tagdata;
    }
}

private function tagdata_callback($matches) {
    $method = $matches[1];
    return $this->$method();
}

注意事项:

  • 这可以制作更混乱的模板。
  • 维护一个公共方法列表显然需要{4}中没有的Reflection。您当然可以手动维护一个预期方法列表。