Twig 细枝/木材有条件显示车身等级

Twig 细枝/木材有条件显示车身等级,twig,timber,Twig,Timber,我正在寻找一种方法,如果URL中有一个特定的字符串,可以将一个类添加到主体中 我已经有了一些逻辑,但是以下语法: 1.天气不干燥 2.由于if语句,仅循环第一次迭代 我想我需要在'if'语句之后放置一个数组,但不确定语法。 任何帮助都将不胜感激。 谢谢 {%if'bus data'在post.link%} {%elseif'总线数据'不在post.link%} {%endif%} {%if post.link%中的“滑行数据”} {%elseif'taxi data'不在post.link%}

我正在寻找一种方法,如果URL中有一个特定的字符串,可以将一个类添加到主体中

我已经有了一些逻辑,但是以下语法: 1.天气不干燥 2.由于if语句,仅循环第一次迭代

我想我需要在'if'语句之后放置一个数组,但不确定语法。 任何帮助都将不胜感激。 谢谢

{%if'bus data'在post.link%}
{%elseif'总线数据'不在post.link%}
{%endif%}
{%if post.link%中的“滑行数据”}
{%elseif'taxi data'不在post.link%}
{%endif%}
{%if post.link%中的“教育数据”}
{%elseif“教育数据”不在post.link%}
{%endif%}
{%if post.link%中的“公共数据”}
{%elseif'public data'不在post.link%}
{%endif%}

您可以创建一个新的过滤器来解决此问题

滤器

小枝


看来我可能把一些非常简单的事情复杂化了,离开它会让事情变得更清楚。 我刚刚整理了我的“else-if”循环:

{% if 'bus-data' in post.link %}
  <body class="{{body_class}} jumbo" data-template="base.twig">
{% elseif 'taxi-data' in post.link %}
  <body class="{{body_class}} jumbo" data-template="base.twig">
{% elseif 'education-data' in post.link %}
  <body class="{{body_class}} jumbo" data-template="base.twig">
{% elseif 'public-data' in post.link %}
  <body class="{{body_class}} jumbo" data-template="base.twig">
{% else %}
  <body class="{{body_class}}" data-template="base.twig">
{% endif %}
{%if'bus data'在post.link%}
{%elseif post.link%中的“滑行数据”}
{%elseif post.link%中的“教育数据”}
{%elseif post.link%中的“公共数据”}
{%else%}
{%endif%}

感谢您的回复-尽管我不确定如何将其实现为“Timber”语法。我选择了下面一个更简单的解决方案。
$twig->addFilter(new Twig_SimpleFilter('contains', function ($value, $needles) {
    if (!is_array($needles)) $needles = [ $needles, ];
    foreach($needles as $needle) if (strpos($value, $needle) !== false) return true;
    return false;
});
<body class="{{body_class}}{% if post.link|contains(['bus-data', 'taxi-data', 'education-data', 'public-data',]) %} jumbo{% endif %}" data-template="base.twig">
{% if 'bus-data' in post.link %}
  <body class="{{body_class}} jumbo" data-template="base.twig">
{% elseif 'taxi-data' in post.link %}
  <body class="{{body_class}} jumbo" data-template="base.twig">
{% elseif 'education-data' in post.link %}
  <body class="{{body_class}} jumbo" data-template="base.twig">
{% elseif 'public-data' in post.link %}
  <body class="{{body_class}} jumbo" data-template="base.twig">
{% else %}
  <body class="{{body_class}}" data-template="base.twig">
{% endif %}