Created
March 29, 2021 11:33
-
-
Save brianlarson/d25d143dd6bb0292a31e820540e53311 to your computer and use it in GitHub Desktop.
Get YouTube or VImeo embed from URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# 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