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.
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
#out ⇒ Object
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
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ramaze/gestalt.rb', line 91 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
78 79 80 81 82 83 84 85 86 |
# File 'lib/ramaze/gestalt.rb', line 78 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
107 108 109 110 111 112 113 |
# File 'lib/ramaze/gestalt.rb', line 107 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.
74 75 76 |
# File 'lib/ramaze/gestalt.rb', line 74 def p(*args, &block) _gestalt_call_tag :p, args, &block end |
#tag(name, *args, &block) ⇒ Object
115 116 117 |
# File 'lib/ramaze/gestalt.rb', line 115 def tag(name, *args, &block) _gestalt_call_tag(name.to_s, args, &block) end |
#to_s ⇒ Object Also known as: to_str
119 120 121 |
# File 'lib/ramaze/gestalt.rb', line 119 def to_s @out.join end |