我想有条件地从宏观回报,这样的伪代码返回:嫩枝:有条件地从宏观
{% macro example() %}
...
{% if condition %} {% return %} {% endif %}
...
{% endmacro %}
这是可能的嫩枝,或许还有一个枝条延伸?
我想有条件地从宏观回报,这样的伪代码返回:嫩枝:有条件地从宏观
{% macro example() %}
...
{% if condition %} {% return %} {% endif %}
...
{% endmacro %}
这是可能的嫩枝,或许还有一个枝条延伸?
有在树枝没有回报,但是你可以用宏的其余部分在if not condition
块这样的:
{% macro example() %}
...
{% if not condition %}
...
{% endif %}
{% endmacro %}
请记住,树枝是一个模板语言。
你知道吗?可以通过编写Twig扩展来创建回报? –
我不知道树枝内部足以回答这个问题。 – Maerlyn
{% macro example(isTrue) %}
{% if isTrue %}
10
{% else %}
20
{% endif %}
{% endmacro %}
{% import _self as self %}
{% set aaa = 3 + self.example(true) ~ '' %}
{{ aaa }}
{% set bbb = 3 + self.example(false) ~ '' %}
{{ bbb }}
输出:
13
23
这里是例子:
{# macro #}
{% macro check(isTrue) %}
{% if isTrue %}
show-paywall
{% else %}
show-content
{% endif %}
{% endmacro %}
{# usage #}
{% import _self as self %}
{% set per = self.check(true)|trim %}
{% if per == 'show-paywall' %}
{{'Show paywall man.'}}
{% else %}
{{ 'You are good man.' }}
{% endif %}
你可以玩在线: https://twigfiddle.com/etn6ef
[这是不好用的宏返回值(HTTPS: //github.com/fabpot/Twig/issues/1497#issuecomment-54506873) – moldcraft