Module: Nanoc::Toolbox::Helpers::HtmlTag

Included in:
Disqus, GithubGist, GoogleAnalytics, GoogleUA, Gravatar, Navigation
Defined in:
lib/nanoc/toolbox/helpers/html_tag.rb

Overview

NANOC Helper for HTML Tags

This module contains functions for generating simple tags with attribute

Author:

Instance Method Summary collapse

Instance Method Details

#content_tag(name, content, options = {}) ⇒ String

Content tag

Examples:

(:p, "Hello world!")
 # => <p>Hello world!</p>
(:div, (:p, "Hello world!"), :class => "strong")
 # => <div class="strong"><p>Hello world!</p></div>

Parameters:

  • name (String)

    the tag name

  • options (Hash) (defaults to: {})

    options to set to the html element

  • open (Boolean)

    flag to set if the tag is self closing or not

Returns:

  • (String)

    the requested tag string



37
38
39
# File 'lib/nanoc/toolbox/helpers/html_tag.rb', line 37

def (name, content, options={})
  "<#{name}#{tag_options(options) if options}>#{content}</#{name}>"
end

#tag(name, options = {}, open = false) ⇒ String

Simple tag

Examples:

tag("br") # => <br />
tag("hr", class => "thin", true) # => <br class="thin">
tag("input", :type => 'text') # => <input type="text" />

Parameters:

  • name (String)

    the tag name

  • options (Hash) (defaults to: {})

    options to set to the html element

  • open (Boolean) (defaults to: false)

    flag to set if the tag is self closing or not

Returns:

  • (String)

    the requested tag string



21
22
23
# File 'lib/nanoc/toolbox/helpers/html_tag.rb', line 21

def tag(name, options={}, open=false)
  "<#{name}#{tag_options(options) if options}#{open ? ">" : " />"}"
end

#tag_options(options) ⇒ Object (protected)



42
43
44
45
46
47
48
49
50
# File 'lib/nanoc/toolbox/helpers/html_tag.rb', line 42

def tag_options(options)
  unless options.empty?
    attributes = []
    options.each do |key, value|
      attributes << %(#{key}="#{value}")
    end
    ' ' + attributes.join(' ')
  end
end