class Templet::Renderers::Tag
Renders an XML tag
Public Class Methods
call(*args, &block)
click to toggle source
A shortcut for calling directly.
# File lib/templet/renderers/tag.rb, line 11 def self.call(*args, &block) new(args.shift).(*args, &block) end
Public Instance Methods
call(*args, **atts) { || ... }
click to toggle source
args
-
The first element becomes the tag's name.
args
-
The remaining elements are the tag's body and/or HTML class.
atts
-
An optional final (Hash) argument are rendered as the tag's attributes.
If a block is given: The tag's body is what the block returns If there's an argument (String Symbol) then it's added to the tag's HTML class.
If NO block is given: The tag's body is the first argument. If there's a second (String Symbol) argument it's added to the tag's HTML class.
Note that in attribute (and tag) names underscores are replaced with dashes.
# File lib/templet/renderers/tag.rb, line 31 def call(*args, **atts) content, html_class = block_given? ? [ yield, args.first ] : args attributes = set_html_class all_atts(atts), html_class render tag_name, content, attributes end
Also aliased as: to_s
Private Instance Methods
all_atts(atts)
click to toggle source
# File lib/templet/renderers/tag.rb, line 68 def all_atts(atts) (default_atts || {}).merge atts end
atts_to_s(atts)
click to toggle source
# File lib/templet/renderers/tag.rb, line 62 def atts_to_s(atts) atts.reduce '' do |acc, (key, value)| acc << " #{dashit key}='#{dash_symbol value}'" end end
dash_symbol(value)
click to toggle source
# File lib/templet/renderers/tag.rb, line 58 def dash_symbol(value) Symbol === value ? dashit(value) : value end
dashit(name)
click to toggle source
Change underscores to dashes when specifying
XML tag names, attribute names and html classes.
For a single underscore, put in two.
# File lib/templet/renderers/tag.rb, line 54 def dashit(name) (name || '').to_s.tr('_', '-').gsub(/--/, '_') # could be better! end
existing_html_class(atts)
click to toggle source
# File lib/templet/renderers/tag.rb, line 80 def existing_html_class(atts) atts.key?(:class) ? "#{dash_symbol atts[:class]} " : '' end
render(name, content, **atts)
click to toggle source
# File lib/templet/renderers/tag.rb, line 43 def render(name, content, **atts) name = dashit name.to_s.sub(/^_+/, '') content = ListPresenter.new.(content) "<#{name}#{atts_to_s atts}>#{content}</#{name}>#{EOL}" end
set_html_class(atts, html_class)
click to toggle source
If there's already an HTML class then append to it
# File lib/templet/renderers/tag.rb, line 73 def set_html_class(atts, html_class) unless (new_html_class = dashit(html_class)).empty? atts[:class] = existing_html_class(atts) << new_html_class end atts end