Skip to content

Instantly share code, notes, and snippets.

@SteveBenner
Last active June 14, 2022 05:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SteveBenner/9029034 to your computer and use it in GitHub Desktop.
Save SteveBenner/9029034 to your computer and use it in GitHub Desktop.
Slim partial - HTML5 video tag
/ Generate an HTML5 video tag with embedded Flash fallback
/ @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video HTML5 video tag spec
/
/ @local [Hash] video Attribute hash for the `video` tag
/ @option video [Integer] :width (320) Width of the video tag in pixels
/ @option video [Integer] :height (240) Height of the video tag in pixels
/
/ @local [Hash] src Mapping of source file types to URLs for source files
/ @option src [String] :mp4 URL of an MP4 source file for the video
/ @option src [String] :webm URL of a WebM source file for the video
/ @option src [String] :ogg URL of an Ogg source file for the video
/
/ @local [Hash] flash Data for a Flash object
/ @option flash [String] :src URL of a flash video to supply as a fallback
/ @option flash [String] :swf URL of a flash video to supply as a fallback
/ @option flash [Hash] :vars Additional data for the Flash object, formatted as URL params
/
/ Slims 'splat' attribute parsing lets us neatly convert a Hash into HTML attributes
/
video*{ width: 320, height: 240, class: 'video-player' }.update(video)
- src.each do |format, file|
source type="video/#{format}" src=file
- if defined?(flash)
object*{ width: video[:width] || 320, height: video[:height] || 240,
type: 'application/x-shockwave-flash', data: flash[:swf] }
param name="movie" value=flash[:swf]
- if flash[:vars]
param name="flashvars" value=(flash[:vars].collect { |k, v| "#{k}=#{v}" }.join('&'))
video:
width: 480
height: auto
poster: path-to-thumbnail.jpg
controls: true
src:
mp4: path-to-video.mp4
webm: path-to-video.webm
ogg: path-to-video.ogv
<video class="video-player" controls="" height="auto" poster="../img/mdc-promo-vid-thumb.jpg" width="480">
<source src="video/mdc-promo-vid.mp4" type="video/mp4" />
<source src="video/mdc-promo-vid.webm" type="video/webm" />
<source src="video/mdc-promo-vid.ogv" type="video/ogg" />
</video>
Copy link

ghost commented Apr 27, 2014

Here is an example of how to include this partial in a framework such as Middleman (the one I use):

= partial('partials/media-element-player', locals: { \
                 video: { \
                   width: '480', height: 'auto', \
                   poster: '../img/mdc-promo-vid-thumb.jpg', \
                   controls: true }, \
                 src: { \
                   mp4:  'video/mdc-promo-vid.mp4',  \
                   webm: 'video/mdc-promo-vid.webm', \
                   ogg:  'video/mdc-promo-vid.ogv' } })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment