Class: Ramaze::Gestalt
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
-
#out ⇒ Object
Returns the value of attribute out.
Class Method Summary collapse
-
.build(&block) ⇒ Object
The default way to start building your markup.
Instance Method Summary collapse
-
#_gestalt_build_tag(name, attr = {}, text = []) ⇒ Object
build a tag for ‘name`, using `args` and an optional block that will be yielded.
- #_gestalt_call_tag(name, args, &block) ⇒ Object
- #_gestalt_escape_entities(s) ⇒ Object
-
#initialize(&block) ⇒ Gestalt
constructor
Gestalt.new is like ::build but will return itself.
-
#method_missing(meth, *args, &block) ⇒ Object
catching all the tags.
-
#p(*args, &block) ⇒ Object
workaround for Kernel#p to make <p /> tags possible.
- #tag(name, *args, &block) ⇒ Object
- #to_s ⇒ Object (also: #to_str)
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.
66 67 68 69 |
# File 'lib/ramaze/gestalt.rb', line 66 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
73 74 75 |
# File 'lib/ramaze/gestalt.rb', line 73 def method_missing meth, *args, &block _gestalt_call_tag meth, args, &block end |
Instance Attribute Details
#out ⇒ Object
Returns the value of attribute out.
37 38 39 |
# File 'lib/ramaze/gestalt.rb', line 37 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
56 57 58 |
# File 'lib/ramaze/gestalt.rb', line 56 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
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/ramaze/gestalt.rb', line 96 def _gestalt_build_tag name, attr={}, text=[] @out << "<#{name}" @out << attr.inject(''){ |s,v| s << %{ #{v[0]}="#{_gestalt_escape_entities(v[1])}"} } 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 |
# 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 else # no attributes, but text _gestalt_build_tag name, {}, args, &block end end |
#_gestalt_escape_entities(s) ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/ramaze/gestalt.rb', line 112 def _gestalt_escape_entities(s) s.to_s.gsub(/&/, '&'). gsub(/"/, '"'). gsub(/'/, '''). gsub(/</, '<'). gsub(/>/, '>') end |
#p(*args, &block) ⇒ Object
workaround for Kernel#p to make <p /> tags possible.
79 80 81 |
# File 'lib/ramaze/gestalt.rb', line 79 def p *args, &block _gestalt_call_tag :p, args, &block end |
#tag(name, *args, &block) ⇒ Object
120 121 122 |
# File 'lib/ramaze/gestalt.rb', line 120 def tag(name, *args, &block) _gestalt_call_tag(name.to_s, args, &block) end |
#to_s ⇒ Object Also known as: to_str
124 125 126 |
# File 'lib/ramaze/gestalt.rb', line 124 def to_s @out.to_s end |