class Sinatra::Exstatic::Tag

For creating HTML tags.

Attributes

attributes[R]
name[R]
options[R]

Public Class Methods

new( name, options={}, &block ) click to toggle source

@param [String] name The tag name e.g. `link`. @param [Hash] options With the exception of any options listed here, these are passed to the tag to make the HTML attributes. @option options [TrueClass] :closed Whether to self-close the link XHTML style or not. @param [#call] block The contents of the block are wrapped by the HTML tag e.g. <p>This is from the block</p> @example

Tag.new "img", {src: "/images/foo.jpg", width: "500"}
# => "<img src="/images/foo.jpg" width="500" />"
Calls superclass method
# File lib/sinatra/exstatic_assets.rb, line 20
def initialize( name, options={}, &block )
  @name       = name
  @closed = (c = options.delete(:closed)).nil? ? true : c
  @options    = options
  @attributes = self.class.make_attributes @options
  @block      = block
  super tag
end

Private Class Methods

make_attributes( options ) click to toggle source

Takes a hash and transforms it into a string of HTML attributes. @param [Hash] options @return [String]

# File lib/sinatra/exstatic_assets.rb, line 51
def self.make_attributes( options )
  options.sort
         .map {|key, value| %(#{key}="#{value}") }
         .join " "
end

Private Instance Methods

tag() click to toggle source

@yield Its return value is used as the contents of the HTML tag.

# File lib/sinatra/exstatic_assets.rb, line 35
def tag
  return @tag if @tag
  start_tag = "<#{@name} #{@attributes}".strip
  @tag = if @block
    "#{start_tag}>#{@block.call}</#{name}>"
  elsif @closed
    "#{start_tag} />"
  else
    "#{start_tag}>"
  end
end