0

我正在使用 Mustache for php 来呈现我的网页。我一直在github上的 dev-branch 中玩代码,发现了一些有趣的操作方法。例如,我知道可以通过为它们定义一个辅助函数来动态加载部分。然而,我似乎找不到的是一种像这样进行嵌套标签的方法:

{{article.{{page.name}}}}

在某种辅助函数或高阶函数的帮助下,这是否可能?

4

1 回答 1

0

You can use Lambdas to achieve something similar. Quoting from Mustache.php's wiki:

When the value is callable — such as an anonymous function — the callable will be invoked and passed the block of text.

So, in your mustache template, the following:

{{#article_field}}{{page.name}}{{/article_field}}

With a View Class like this:

class View_Page {

    public $_article;

    public function article_field()
    {
        return function($string)
        {
            return $this->_article[$string];
        }
    }
}

Will output the article field whose name resides in the page.name property's value.

Head over to Mustache.php's official wiki to read more about Lambdas and callables.

于 2013-01-10T17:15:59.540 回答