`base_template_class` php twig模板的替代方案

发布于 2025-02-04 17:41:18 字数 1292 浏览 3 评论 0原文

从#3.0.0-beta1(2019-11-11)上的更改日志中,

  • 删除了twig \环境上的“ base_template_class”选项,

我的自定义模板类从twig> twig \ template> 覆盖display()render()函数

public function display(array $context, array $blocks = null)
    {
        $env = $this->env;
        $env->addTemplateStack($this->getTemplateName());

        if (is_array($blocks))
        {
           
            $env->current_inheritance_level++;
            $this->inheritance_level = $env->current_inheritance_level;
            $env->active_inheritance_level = $env->current_inheritance_level;
            parent::display($context, $blocks);
        }
        else {
            // Call to the deepest template or include
            parent::display($context, []);
        }
    }
public function render(array $context)
{
    $env = $this->env;

    $this->inheritance_level = 0;
    $env->active_inheritance_level = 0;
    $env->current_inheritance_level = 0;

    return parent::render($context);
}

,但使用twig v3 base_template_class_class选项已删除选项 因此,我必须使用原始显示&渲染功能。 在V3上拥有此类数据的替代方法是什么

From the change log on # 3.0.0-BETA1 (2019-11-11)

  • removed the "base_template_class" option on Twig\Environment

I have a custom Template class extend from Twig\Template
which override the display() and render() function

public function display(array $context, array $blocks = null)
    {
        $env = $this->env;
        $env->addTemplateStack($this->getTemplateName());

        if (is_array($blocks))
        {
           
            $env->current_inheritance_level++;
            $this->inheritance_level = $env->current_inheritance_level;
            $env->active_inheritance_level = $env->current_inheritance_level;
            parent::display($context, $blocks);
        }
        else {
            // Call to the deepest template or include
            parent::display($context, []);
        }
    }
public function render(array $context)
{
    $env = $this->env;

    $this->inheritance_level = 0;
    $env->active_inheritance_level = 0;
    $env->current_inheritance_level = 0;

    return parent::render($context);
}

But with Twig V3 base_template_class option was removed
So I have to use the original display & render function instead.
What is the alternative to having these kinds of data on V3

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文