使用可变语法(不带“>")的小胡子部分吗? [英] Mustache partials using variable syntax (without the ">")?

查看:107
本文介绍了使用可变语法(不带“>")的小胡子部分吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将胡须(php版本)用作模板引擎.

I'm using mustache (php version) as my templating engine.

我想知道是否有可能将{{something}}用作部分内容,而不是必须在模板中将其格式化为{{> something}}.理想情况下,如果变量名称位于_partials数组中,则将其视为部分变量.

I'm wondering if it's possible to have {{something}} serve as a partial, instead of having to format it as {{>something}} in the template. Ideally, a variable would be treated as a partial if the variable name is in the _partials array.

这将使我可以将变量更改为部分变量,而不必对模板进行任何更改.

This would allow me to change a variable to a partial without having to make any changes to templates.

这可能吗?

推荐答案

我想出了如何通过修改Mustache.php中的_renderTag函数来做到这一点的方法.在switch语句中,对于默认情况,我只是检查$ tag_name是否在$ this-> _ partials数组中.

I figured out how to do this by modifying the _renderTag function in Mustache.php. In the switch statement, for the default case, I just check to see if $tag_name is in the $this->_partials array.

protected function _renderTag($modifier, $tag_name, $leading, $trailing) {
    switch ($modifier) {
        case '=':
            return $this->_changeDelimiter($tag_name, $leading, $trailing);
            break;
        case '!':
            return $this->_renderComment($tag_name, $leading, $trailing);
            break;
        case '>':
        case '<':
            return $this->_renderPartial($tag_name, $leading, $trailing);
            break;
        case '{':
            // strip the trailing } ...
            if ($tag_name[(strlen($tag_name) - 1)] == '}') {
                $tag_name = substr($tag_name, 0, -1);
            }
        case '&':
            if ($this->_hasPragma(self::PRAGMA_UNESCAPED)) {
                return $this->_renderEscaped($tag_name, $leading, $trailing);
            } else {
                return $this->_renderUnescaped($tag_name, $leading, $trailing);
            }
            break;
        case '#':
        case '^':
        case '/':
            // remove any leftover section tags
            return $leading . $trailing;
            break;
        default:
            // Render var as partial if it is in _partial array (so we don't have to use "{>partial}" syntax)
            if ($this->_partials[$tag_name]) {
                $partial = $this->_renderPartial($tag_name, $leading, $trailing);
                return $partial;
            }


            if ($this->_hasPragma(self::PRAGMA_UNESCAPED)) {
                return $this->_renderUnescaped($modifier . $tag_name, $leading, $trailing);
            } else {
                return $this->_renderEscaped($modifier . $tag_name, $leading, $trailing);
            }
            break;
    }
}

这篇关于使用可变语法(不带“&gt;")的小胡子部分吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆