Permalink
Browse files

Add video tag helper

hanami/hanami#396

This commit exposes a `video` method to the view
which can be used to generate HTML5 video tags.
It accepts options to generate HTML attributes along
with a block for creating fallback content and/or
`source` and `track` tags.
  • Loading branch information...
leighhalliday authored and jodosha committed Dec 1, 2015
1 parent e837894 commit dfc52ea8166b97142c2362462e40c8b745ab5138
Showing with 63 additions and 0 deletions.
  1. +63 −0 asset_tag_helpers.rb
View
@@ -40,6 +40,69 @@ def image(source, options = {})
def asset_path(source)
"/assets/#{source}" # To be implemented
end
# Generates a video tag for the given arguments.
#
# @raise [ArgumentError] if the signature isn't respected
# @since x.x.x
# @api public
#
# @example Basic usage
# <%= video('movie.mp4') %>
# # => <video src="/assets/movie.mp4"></video>
#
# @example HTML attributes
# <%= video('movie.mp4', autoplay: true, controls: true) %>
# # => <video src="/assets/movie.mp4" autoplay="autoplay" controls="controls"></video>
#
# @example Fallback Content
# <%=
# video('movie.mp4') do
# "Your browser does not support the video tag"
# end
# %>
# # => <video src="/assets/movie.mp4">\nYour browser does not support the video tag\n</video>
#
# @example Tracks
# <%=
# video('movie.mp4') do
# track kind: 'captions', src: view.asset_path('movie.en.vtt'), srclang: 'en', label: 'English'
# end
# %>
# # => <video src="/assets/movie.mp4">\n<track kind="captions" src="/assets/movie.en.vtt" srclang="en" label="English">\n</video>
#
# @example Sources
# <%=
# video do
# text "Your browser does not support the video tag"
# source src: view.asset_path('movie.mp4'), type: 'video/mp4'
# source src: view.asset_path('movie.ogg'), type: 'video/ogg'
# end
# %>
# # => <video>\nYour browser does not support the video tag\n<source src="/assets/movie.mp4" type="video/mp4">\n<source src="/assets/movie.ogg" type="video/ogg">\n</video>
#
# @example Without any argument
# <%= video %>
# # => ArgumentError
#
# @example Without src and without block
# <%= video(content: true) %>
# # => ArgumentError
def video(src = nil, options = {}, &blk)
options ||= {}
if src.respond_to?(:to_hash)
options = src.to_hash
elsif src
options[:src] = asset_path(src)
end
if !options[:src] && !block_given?
raise ArgumentError.new('You should provide a source via `src` option or with a `source` HTML tag')
end
html.video(blk, options)
end
end
end
end

0 comments on commit dfc52ea

Please sign in to comment.