Module: Tagz::Helpers

Defined in:
lib/tagz/helpers.rb

Overview

Helpers

Tagz::Helpers consists of methods which assist creation of common tag combinations and standards. For example images may simply call image(‘foo.png’), as a shortcut for tag(:img, :src => ‘foo.png’).

Class Method Summary collapse

Class Method Details

.cdata(contents) ⇒ Object

Return CDATA tag with contents.

Examples

cdata '<foo>'
# => <![CDATA[<foo>]]>


102
103
104
# File 'lib/tagz/helpers.rb', line 102

def cdata contents
  "<![CDATA[#{contents}]]>"
end

.image(path, attrs = {}) ⇒ Object

Return image tag to path.

Examples

image 'foo.png'
# => <img src="foo.png" />

image 'foo.png', :alt => 'Kung-foo'
# => <img src="foo.png" alt="Kung-foo">


28
29
30
# File 'lib/tagz/helpers.rb', line 28

def image path, attrs = {}
  tag :img, { :src => path }.merge(attrs)
end

.javascript(path = nil, attrs = {}, &block) ⇒ Object

Return script tag to path. When a block is passed, a script tag will be created with the yielded value as its contents.

Examples

javascript do
  "foo"
end
# => <script type="text/javascript">foo</script>

javascript 'jquery.js'
# => <script type="text/javascript" src="jquery.js"></script>


72
73
74
75
# File 'lib/tagz/helpers.rb', line 72

def javascript path = nil, attrs = {}, &block
  contents = yield if block
  tag :script, contents, { :type => 'text/javascript', :src => path }.merge(attrs)
end

.meta(name, contents) ⇒ Object

Return meta tag name with contents.

Examples

meta :keywords, 'foo bar'
meta :description, 'Welcome to foo bar'

# => <meta name="keywords" contents="foo bar">
# => <meta name="description" contents="Welcome to foo bar">


89
90
91
# File 'lib/tagz/helpers.rb', line 89

def meta name, contents
  tag :meta, :name => name, :contents => contents 
end

.stylesheet(path = nil, attrs = {}, &block) ⇒ Object

Return stylesheet link tag to path. When a block is passed, a style tag will be created with the yielded value as its contents.

Examples

stylesheet do
  "body {
    color: blue;
  }"
end
# => <style>body { ... }</style>

stylesheet 'style.css', :media => :print
# => <link rel="stylesheet" href="style.css" media="print" />


51
52
53
54
# File 'lib/tagz/helpers.rb', line 51

def stylesheet path = nil, attrs = {}, &block
  return tag(:style, yield, { :type => 'text/css' }.merge(attrs)) if block
  tag :link, { :rel => 'stylesheet', :href => path }.merge(attrs)
end