使用变量语法的小胡子部分(没有“>”)?

发布于 2024-11-19 15:55:52 字数 204 浏览 4 评论 0原文

我使用 Mustache (PHP 版本)作为我的模板引擎。

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

这将允许我将变量更改为部分变量,而无需对模板进行任何更改。

这可能吗?

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

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.

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紧拥背影 2024-11-26 15:55:52

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

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;
    }
}

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文