Class: Malline::Template
- Inherits:
-
Object
- Object
- Malline::Template
- Defined in:
- lib/malline/template.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#path ⇒ Object
Returns the value of attribute path.
-
#short_tag_excludes ⇒ Object
Returns the value of attribute short_tag_excludes.
-
#whitespace ⇒ Object
Returns the value of attribute whitespace.
Class Method Summary collapse
-
.html_escape(s) ⇒ Object
These two are stolen from ERB © 1999-2000,2002,2003 Masatoshi SEKI.
- .url_encode(s) ⇒ Object
Instance Method Summary collapse
- #add_text(*values) ⇒ Object
- #add_unescaped_text(value) ⇒ Object
-
#execute(dom, tpl = nil, &block) ⇒ Object
Changes dom to active @dom, and executes tpl / block.
- #helper(helper, *args, &block) ⇒ Object
-
#initialize(view, opts) ⇒ Template
constructor
A new instance of Template.
-
#render(dom = nil) ⇒ Object
Render the xml tree at dom or root.
-
#run(tpl = nil, &block) ⇒ Object
Execute and render a text or block.
- #tag(s, *args, &block) ⇒ Object
Constructor Details
#initialize(view, opts) ⇒ Template
Returns a new instance of Template.
25 26 27 28 29 30 31 |
# File 'lib/malline/template.rb', line 25 def initialize view, opts @view = view @whitespace = false @path = 'Malline template' @options = opts @short_tag_excludes = [] end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
20 21 22 |
# File 'lib/malline/template.rb', line 20 def @options end |
#path ⇒ Object
Returns the value of attribute path.
23 24 25 |
# File 'lib/malline/template.rb', line 23 def path @path end |
#short_tag_excludes ⇒ Object
Returns the value of attribute short_tag_excludes.
21 22 23 |
# File 'lib/malline/template.rb', line 21 def short_tag_excludes @short_tag_excludes end |
#whitespace ⇒ Object
Returns the value of attribute whitespace.
22 23 24 |
# File 'lib/malline/template.rb', line 22 def whitespace @whitespace end |
Class Method Details
.html_escape(s) ⇒ Object
These two are stolen from ERB © 1999-2000,2002,2003 Masatoshi SEKI
35 36 37 |
# File 'lib/malline/template.rb', line 35 def self.html_escape(s) s.to_s.gsub(/&/, "&").gsub(/\"/, """).gsub(/>/, ">").gsub(/</, "<") end |
.url_encode(s) ⇒ Object
38 39 40 |
# File 'lib/malline/template.rb', line 38 def self.url_encode(s) s.to_s.gsub(/[^a-zA-Z0-9_\-.]/n){ sprintf("%%%02X", $&.unpack("C")[0]) } end |
Instance Method Details
#add_text(*values) ⇒ Object
54 55 56 57 |
# File 'lib/malline/template.rb', line 54 def add_text *values @dom << ' ' if @whitespace @dom << Template.html_escape(values.join(' ')) end |
#add_unescaped_text(value) ⇒ Object
59 60 61 62 |
# File 'lib/malline/template.rb', line 59 def add_unescaped_text value @dom << ' ' if @whitespace @dom << value.to_s unless value.nil? end |
#execute(dom, tpl = nil, &block) ⇒ Object
Changes dom to active @dom, and executes tpl / block
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/malline/template.rb', line 43 def execute dom, tpl = nil, &block tmp = @dom @dom = dom if block_given? @view.instance_eval &block else @view.instance_eval tpl, @path end @dom = tmp end |
#helper(helper, *args, &block) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/malline/template.rb', line 64 def helper helper, *args, &block tmp = @view.send(helper, *args, &block) @dom << ' ' if @whitespace @dom << tmp.to_s tmp end |
#render(dom = nil) ⇒ Object
Render the xml tree at dom or root
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/malline/template.rb', line 93 def render dom = nil (dom || @dom).inject('') do |out, tag| if tag.is_a?(String) out << tag else out << ' ' if tag[:whitespace] out << "<#{tag[:name]}" out << tag[:attrs].inject(''){|s, a| s += " #{a.first}=\"#{Template.html_escape(a.last)}\""} if tag[:children].empty? if @short_tag_excludes.include?(tag[:name]) out << "></#{tag[:name]}>" else out << '/>' end else out << '>' out << render(tag[:children]) out << "</#{tag[:name]}>" end end end end |
#run(tpl = nil, &block) ⇒ Object
Execute and render a text or block
118 119 120 121 122 |
# File 'lib/malline/template.rb', line 118 def run tpl = nil, &block tmp = [] execute tmp, tpl, &block render tmp end |
#tag(s, *args, &block) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/malline/template.rb', line 71 def tag s, *args, &block tag = { :name => s.to_s, :attrs => {}, :children => [] } tag[:whitespace] = true if @whitespace whitespace = @whitespace @whitespace = true if args.delete(:whitespace) if args.last.is_a?(Hash) tag[:attrs].merge!(args.pop) end txt = args.flatten.join('') tag[:children] << Template.html_escape(txt) unless txt.empty? @dom << tag execute tag[:children], &block if block_given? @whitespace = whitespace ViewProxy.new self, tag end |