Twig extend template on condition

前端 未结 5 1019
刺人心
刺人心 2020-12-14 00:23

I use Symfony 2 with Twig and my question is pretty straightforward:

In a view I want to extend one of the layouts based on a variable. If the variable is fals

5条回答
  •  一向
    一向 (楼主)
    2020-12-14 00:56

    Answer from the official documentation:

    Conditional Inheritance

    As the template name for the parent can be any valid Twig expression, it's possible to make the inheritance mechanism conditional:

    {% extends standalone ? "minimum.html" : "base.html" %}
    

    In this example, the template will extend the "minimum.html" layout template if the standalone variable evaluates to true, and "base.html" otherwise.

提交回复
热议问题