This solution utilises the ability to pass parameters to included files. You can pass the video ID to a generic YouTube embed code used on all pages.
Create file _includes/youtube.html containing the following code:
Note: this uses the “privacy enhanced mode” and only the YouTube video ID has been parametrised.
To embed a Youtube video, use this in your posts:
For maintenance purposes, you want to be able to modify and/or update the HTML snippet used for all YouTube videos.
I prefer not to add tons of custom code to Jekyll even though there are several Jekyll YouTube plugins out there. To me this is an easier solution that requires zero plugins. Due to its simplicity, this method can easily be repurposed for other embeds such as Vimeo.
This snippet does not take into account videos with different aspect ratios. In that case, you may want to extend the code a bit.
- “Passing parameters to inclusion in Liquid templates” on Stack Overflow.
- Image courtesy of Jekyll and YouTube.