2

我正在尝试在 Twig 的三元语句中显示 href,该语句使用标准 if else 语句,如下所示:

{% if news_count|length > 0 %}
    <a href="/{{profile.profile_id}}/news/">News {{news_count}}</a>
{% else %}
    News 0
{% endif %}

尝试如下使用三元语句时,我无法正确格式化链接。

{{ (news_count|length > 0) ? '<a href="/' ~ profile.profile_id ~ '/news/">News ' ~ news_count ~ '</a>' : "News 0" }}

这打印:

<a href="/10/news/">News 25</a> News 25

我尝试在 href 值的末尾使用转义和原始过滤器,但没有运气。

4

1 回答 1

3

尝试:

{{ ((news_count|length > 0) ? '<a href="/' ~ profile.profile_id ~ '/news/">News ' ~ news_count ~ '</a>' : "News 0")|raw }}

顺便说一句,似乎 news_count 已经是一个 int 值。所以我假设不需要“|length”。

于 2012-08-13T11:28:05.033 回答