Class: Ramaze::Gestalt

Inherits:
Object show all
Defined in:
lib/ramaze/gestalt.rb

Overview

Gestalt is the custom HTML/XML builder for Ramaze, based on a very simple DSL it will build your markup.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Gestalt

Gestalt.new is like ::build but will return itself. you can either access #out or .to_s it, which will return the actual markup.

Useful for distributed building of one page.



61
62
63
64
# File 'lib/ramaze/gestalt.rb', line 61

def initialize(&block)
  @out = []
  instance_eval(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

catching all the tags. passing it to _gestalt_build_tag



68
69
70
# File 'lib/ramaze/gestalt.rb', line 68

def method_missing(meth, *args, &block)
  _gestalt_call_tag meth, args, &block
end

Instance Attribute Details

#outObject

Returns the value of attribute out.



32
33
34
# File 'lib/ramaze/gestalt.rb', line 32

def out
  @out
end

Class Method Details

.build(&block) ⇒ Object

The default way to start building your markup. Takes a block and returns the markup.

Example:

html =
  Gestalt.build do
    html do
      head do
        title "Hello, World!"
      end
      body do
        h1 "Hello, World!"
      end
    end
  end


51
52
53
# File 'lib/ramaze/gestalt.rb', line 51

def self.build(&block)
  self.new(&block).to_s
end

Instance Method Details

#_gestalt_build_tag(name, attr = {}, text = []) ⇒ Object

build a tag for ‘name`, using `args` and an optional block that will be yielded



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ramaze/gestalt.rb', line 99

def _gestalt_build_tag(name, attr = {}, text = [])
  @out << "<#{name}"
  @out << attr.map{|(k,v)| %[ #{k}="#{_gestalt_escape_entities(v)}"] }.join
  if text != [] or block_given?
    @out << ">"
    @out << _gestalt_escape_entities([text].join)
    if block_given?
      text = yield
      @out << text.to_str if text != @out and text.respond_to?(:to_str)
    end
    @out << "</#{name}>"
  else
    @out << ' />'
  end
end

#_gestalt_call_tag(name, args, &block) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ramaze/gestalt.rb', line 83

def _gestalt_call_tag(name, args, &block)
  if args.size == 1 and args[0].kind_of? Hash
    # args are just attributes, children in block...
    _gestalt_build_tag name, args[0], &block
  elsif args[1].kind_of? Hash
    # args are text and attributes ie. a('mylink', :href => '/mylink')
    _gestalt_build_tag(name, args[1], args[0], &block)
  else
    # no attributes, but text
    _gestalt_build_tag name, {}, args, &block
  end
end

#_gestalt_escape_entities(s) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/ramaze/gestalt.rb', line 115

def _gestalt_escape_entities(s)
  s.to_s.gsub(/&/, '&amp;').
    gsub(/"/, '&quot;').
    gsub(/'/, '&apos;').
    gsub(/</, '&lt;').
    gsub(/>/, '&gt;')
end

#p(*args, &block) ⇒ Object

workaround for Kernel#p to make <p /> tags possible.



74
75
76
# File 'lib/ramaze/gestalt.rb', line 74

def p(*args, &block)
  _gestalt_call_tag :p, args, &block
end

#select(*args, &block) ⇒ Object

workaround for Kernel#select to make <select></select> work



79
80
81
# File 'lib/ramaze/gestalt.rb', line 79

def select(*args, &block)
  _gestalt_call_tag(:select, args, &block)
end

#tag(name, *args, &block) ⇒ Object



123
124
125
# File 'lib/ramaze/gestalt.rb', line 123

def tag(name, *args, &block)
  _gestalt_call_tag(name.to_s, args, &block)
end

#to_sObject Also known as: to_str



127
128
129
# File 'lib/ramaze/gestalt.rb', line 127

def to_s
  @out.join
end