如何有条件地覆盖

如何有条件地覆盖

本文介绍了如何有条件地覆盖 TWIG 布局块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,让我从我尝试使用的代码开始:

First, let me start with the code I'm attempting to use:

{% if modal == true %}
    {% block header %}{% endblock %}
    {% block footer %}{% endblock %}
{% endif %}

我想要完成的是仅当名为 modal 的变量为真时才显示我的页眉和页脚块.我在 if 语句下面也有这个:

What I'm trying to accomplish is to not show my header and footer blocks ONLY if the variable called modal is true. I also have this below the if statement:

{% block content %}
    {{ dump(modal) }}
{% endblock %}

这里发生的事情是,无论 modal 的值是否为真,我用于清空页眉和页脚块的覆盖始终运行.所以,我用传入的模态为 false 来运行它,结果是页眉和页脚仍然没有显示.dump 命令的输出准确地显示 true 或 false,但条件似乎总是在 if 语句中评估为 true.

What happens here is that my override for emptying the header and footer blocks always runs regardless of if the value of modal is true or otherwise. So, I run this with modal passed in as false and the result is that the header and footer still don't show. The output of the dump command accurately shows true or false, but the condition always seems to evaluate to true in the if statement.

块是否可以不包含在条件语句中,或者我需要做些什么才能使其工作?

Can blocks not be wrapped in a conditional statement, or is there something additional I need to do to make this work?

感谢您提供的任何帮助.

Thanks for any help you can offer.

推荐答案

块不关心它周围的任何逻辑,如 文档:

Blocks don't care about any logic around it, as said in the documentation:

块提供了一种方法来更改模板特定部分的呈现方式,但它不会以任何方式干扰其周围的逻辑.

您应该将该逻辑放在块内,而不是放在外面,正如您在该文章的最后一个示例中看到的那样.

You should put that logic inside the block, not on the outerside, as you can see on the last example in that article.

这篇关于如何有条件地覆盖 TWIG 布局块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 01:11