0

I have this two URLs:

https://youtu.be/erwZDijlFAA

https://vimeo.com/262998843

Now i want to check it by Twig. How can i check ?

  {% if url %} <p>youtube</p> {% else %} <p>vimeo</p> {% endif %}

Or have any way to check whether the provided URLs is youtube or vimeo ?

DarkBee
  • 16,592
  • 6
  • 46
  • 58
Sumon Hasan
  • 310
  • 3
  • 14
  • Possible duplicate of [Find substring in the string in TWIG](https://stackoverflow.com/questions/13007288/find-substring-in-the-string-in-twig) – DarkBee Jun 26 '18 at 05:53

2 Answers2

0

You can use a simply version that use starts with operator like:

   {% if url starts with 'https://youtu.be/' %}
    YouTube.com
   {% endif %}

Or for more complex criteria you can use Regular expression thru matches like:

   {% if url matches '%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i' %}
    YouTube.com
   {% endif %}

RegEx credits: https://gist.github.com/ghalusa/6c7f3a00fd2383e5ef33

More info in the doc about comparison operator here

Hope this help

Matteo
  • 37,680
  • 11
  • 100
  • 115
0

You can use the below code to segregate the videos in 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 %}
Lokesh S
  • 426
  • 4
  • 13