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’).

Defined Under Namespace

Modules: Delegates

Class Method Summary collapse

Class Method Details

.cdata(contents) ⇒ Object

Return CDATA tag with contents.

Examples

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


104
105
106
# File 'lib/tagz/helpers.rb', line 104

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">


30
31
32
# File 'lib/tagz/helpers.rb', line 30

def image path, attrs = {}
  Tagz.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>


74
75
76
77
# File 'lib/tagz/helpers.rb', line 74

def javascript path = nil, attrs = {}, &block
  contents = yield if block
  Tagz.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">


91
92
93
# File 'lib/tagz/helpers.rb', line 91

def meta name, contents
  Tagz.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" />


53
54
55
56
# File 'lib/tagz/helpers.rb', line 53

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