在 Twig 的父范围内设置变量 [英] Set variable in parent scope in Twig

查看:31
本文介绍了在 Twig 的父范围内设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Smarty 中你可以做到

In Smarty you can do

{$var = 'bla' scope=parent}

在 Twig 中可以吗?

Is it possible in Twig?

不建议使用积木.我知道.我需要变量.

Don't suggest to use blocks. I know. I need variable.

推荐答案

如果您不想使用 default() 过滤器(即,当您想在整个过程中多次使用变量时您的父模板和子模板),您实际上可以在父模板中定义一个包含整个页面的块,然后将其他块嵌套在其中:

If you don't want to use the default() filter (i.e., when you want to use the variable multiple times throughout your parent and child templates), you can actually define a block that contains your entire page in the parent template, and then nest your other blocks inside of that:

{# base.twig #}

{# Default page properties.  You can override these in the `page` block of your child templates. #}
{% set page = page | default({}) | merge({
    "title"       : "My Default Title",
    "description" : "Default description"
}) %}

{% block page %}
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <meta name="description" content="{{ page.description }}"> 
            <title>{{ page.title }}</title>

            ...

        </head>
        <body>
            {% block content %}{% endblock %}
        </body>
    </html>
{% endblock %}

然后,您可以通过设置值 然后 调用 来覆盖子模板中 page 块中的 page 变量父():

You can then override the page variable in the page block in your child template, by setting the value and then calling parent():

{# child.twig #}

{% extends "base.twig" %}

{% block page %}
    {# By putting this in a special block, we ensure that it will be set AFTER the default values are set in the parent template, 
    but BEFORE the page itself is rendered. #}

    {% set page = page | merge({
        "title"       : "Child Page",
        "description" : "Welcome to the child page!"
    }) %}    

    {{ parent() }}
{% endblock %}

{% block content %}
    ...
{% endblock %}

请注意,在父模板中,我们在 page 块之外定义 page 变量,而在子模板中,我们将其定义在 inside page 块.

Note that in the parent template, we define the page variable outside of the page block, while in the child template we define it inside the page block.

因此,Twig 将执行以下操作:

So, Twig will do the following:

  1. 渲染child.twig时,会从base.twig的顶部开始,为page变量设置默认值.
  2. 当涉及到 page block 时,它会看到 child.twig 覆盖了该块.因此,它将运行 child.twig 中的 page 块.
  3. child.twigpage 块内,它将为 page 变量设置新值.然后它会调用 parent(),告诉它返回 base.twig 并渲染父 page 块.
  4. 然后它将继续呈现页面,替换 child.twig 中定义的任何其他块(在我的示例中,它将呈现 content 块).
  1. When it renders child.twig, it will start from the top of base.twig, setting the default values for the page variable.
  2. When it comes to the page block, it will see that child.twig overrides that block. So, it will run the page block in child.twig instead.
  3. Inside the page block in child.twig, it will set the new values for the page variable. It will then call parent(), which tells it to go back to base.twig and render the parent page block.
  4. It will then continue to render the page, substituting any additional blocks as defined in child.twig (in my example, it will render the content block).

查看一个工作示例这里.请注意,当您开始添加多层继承(例如,孙模板)时,这可能会变得更加复杂.

See a working example here. Do be aware that this can become more complicated when you start adding multiple layers of inheritance (e.g., grandchild templates).

这篇关于在 Twig 的父范围内设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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