您将如何在Twig的{%%}内部执行三元条件

时间:2018-12-10 04:49:03

标签: php twig bolt-cms

我想呈现参数(如果存在),但是找不到正确显示参数并不断获取的方法 打开的括号未正确关闭。值“:”的意外标记“标点”(期望值“)”为“标点”)

其中

{% setcontent records = 'properties' where
{filter:search_term,
((classification) ? ('classification':classification):(''))
} printquery  %}

1 个答案:

答案 0 :(得分:2)

要在Bolt CMS中使用它,请先定义您的选项,然后将其传递给Bolt CMS

{% set options = { filter: search_term , } %}
{% if classification is defined and classification|trim != '' %}
    {% set options = options|merge({classification:classification,}) %}
{% endif %}

{% setcontent records = 'properties' where options printquery  %}

重读您的问题后,您可能正在寻找类似的东西

{% set records %}
'properties' where { 
    filter : '{{ search_term }}',
    classification: '{{ classification is defined ? classification : '' }}',
} printquery  %}
{% endset %}

{{ records }}

但是,在这里使用过滤器default比使用三元运算符更合适

{% set records %}
'properties' where { 
    filter : '{{ search_term }}',
    classification: '{{ classification|default('') }}',
} printquery  %}
{% endset %}

{{ records }}

demo


要省略属性,请使用以下内容,

{% set records %}
'properties' where { 
    filter : '{{ search_term }}',
    {% if classification is defined and classification|trim != '' %}classification: '{{ classification }}',{% endif %}
} printquery  %}
{% endset %}