【发布时间】:2018-12-04 17:46:56
【问题描述】:
我有这两个网址:
现在我想通过 Twig 检查它。我该如何检查?
{% if url %} <p>youtube</p> {% else %} <p>vimeo</p> {% endif %}
或者有什么方法可以检查提供的网址是 youtube 还是 vimeo ?
【问题讨论】:
标签: twig
我有这两个网址:
现在我想通过 Twig 检查它。我该如何检查?
{% if url %} <p>youtube</p> {% else %} <p>vimeo</p> {% endif %}
或者有什么方法可以检查提供的网址是 youtube 还是 vimeo ?
【问题讨论】:
标签: twig
您可以使用使用starts with 运算符的简单版本,例如:
{% if url starts with 'https://youtu.be/' %}
YouTube.com
{% endif %}
或者对于更复杂的条件,您可以通过matches 使用正则表达式,例如:
{% if url matches '%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i' %}
YouTube.com
{% endif %}
RegEx 学分:https://gist.github.com/ghalusa/6c7f3a00fd2383e5ef33
More info in the doc about comparison operator here
希望有帮助
【讨论】:
您可以使用下面的代码来隔离 twig 中的视频。
{% set link_type = view.field.body.original_value %} // Fetching value from the body field
{% if ('youtube' in link_type|render|render) %}
youtube url
{% else if ('vimeo' in link_typr|render|render ) %}
vimeo Url
{% endif %}
【讨论】: