Skip to content

Instantly share code, notes, and snippets.

@brianlarson
Created March 29, 2021 11:33
Show Gist options
  • Save brianlarson/d25d143dd6bb0292a31e820540e53311 to your computer and use it in GitHub Desktop.
Save brianlarson/d25d143dd6bb0292a31e820540e53311 to your computer and use it in GitHub Desktop.
Get YouTube or VImeo embed from URL
{# get video embed - supports vimeo and youtube #}
{# youtube id - https://supergeekery.com/blog/extracting-a-youtube-id-from-a-url-with-twig #}
{%- macro videoEmbed(url) -%}
{%- if 'youtu' in url -%}
{% set ytregex1 = '/^(?:https?:\\/\\/)?(?:www\.)?(?:youtu\.be\\/|youtube\.com)(?:\\/embed\\/|\\/v\\/|\\/watch\\?v=||\\/watch\\\?.+&v=)/' %}
{% set ytregexTrailingSlash = '/(\\/)$/' %}
{% set ytregexTrailingVariables = '/(&+.*)/' %}
{% set id = url | replace(ytregex1, '') | replace(ytregexTrailingSlash, '') | replace(ytregexTrailingVariables, '') %}
<iframe src="https://www.youtube.com/embed/{{ id }}?autoplay=0&rel=0&modestbranding=1&playsinline=1&rel=0" width="1920" height="1080" frameborder="0" allowfullscreen uk-responsive uk-video="autoplay: false;"></iframe>
{%- elseif 'vimeo' in url -%}
{% set url = url|replace({
'vimeo.com/': 'player.vimeo.com/video/'
}) %}
<iframe title="vimeo-player" src="{{ url }}" width="1920" height="1080" frameborder="0" allowfullscreen uk-responsive uk-video="autoplay: false;"></iframe>
{%- endif -%}
{%- endmacro -%}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment