首页 > 解决方案 > 我如何在木材/树枝的一行中写多个“if”条件?

问题描述

我有一种情况,我想在一行中编写多个“if”条件,例如:{% if room_status == 'For Rent' and not 'Booked' %}这显然行不通,但是您可以了解我要完成的工作。

但是不知道如何写这个:

{% if room_status == 'For Rent'%}
  {% if room_status != 'Booked' %}
    {# <Then run some code here> #}
  {% endif %}
{% endif %}

任何建议都非常感谢!

标签: wordpresstwigwordpress-themingtimber

解决方案


来自Twig If文档

对于多个条件,and可以or使用:

{% if temperature > 18 and temperature < 27 %}
    <p>It's a nice day for a walk in the park.</p>
{% endif %}

因此,在您的情况下,这应该有效:

{% if room_status == 'For Rent' and room_status != 'Booked' %}
    {# <Then run some code here> #}
{% endif %}

尽管在我看来您并不真的需要第二次检查。room_status变量只能包含一个值,对吗?它不能同时是“出租”和“已预订”,它要么是一个,要么是另一个。


推荐阅读