Class: Malline::Template
- Inherits:
-
Object
- Object
- Malline::Template
- Defined in:
- lib/malline/template.rb
Overview
This is the class that really evaluates the template and is accessible from the view by “malline”, for example: malline.path = ‘File name’
Instance Attribute Summary collapse
-
#helper_overrides ⇒ Object
Every overriden (in definetags!) helper method (:name => method).
-
#options ⇒ Object
Current options (like @@options in Base).
-
#path ⇒ Object
Current file name.
-
#plugins ⇒ Object
List all installed plugins.
-
#rendered ⇒ Object
readonly
Render result of the last #render.
-
#short_tag_excludes ⇒ Object
List of every tag that doesn’t support self-closing syntax.
-
#tags ⇒ Object
Every available tag, excluding the specific methods (:name => bool).
-
#whitespace ⇒ Object
Current state of :whitespace-modifier (bool).
Class Method Summary collapse
-
.html_escape(s) ⇒ Object
Stolen from ERB, © 1999-2000,2002,2003 Masatoshi SEKI.
-
.url_encode(s) ⇒ Object
Stolen from ERB, © 1999-2000,2002,2003 Masatoshi SEKI.
Instance Method Summary collapse
-
#add_text(*values) ⇒ Object
Add escaped string to @dom.
-
#add_unescaped_text(value) ⇒ Object
Add unescaped string to @dom.
-
#define_tag!(tag) ⇒ Object
Define a method tag.
-
#definetags(*tags) ⇒ Object
Marking tags as usable, but not overriding anything.
-
#definetags!(*tags) ⇒ Object
Define tags as a methods, overriding all same named methods.
-
#execute(dom, tpl = nil, &block) ⇒ Object
Changes dom to active @dom, and executes tpl / block.
-
#helper(helper, *args, &block) ⇒ Object
Call a helper (a method defined outside malline whose output is stored to @dom).
-
#init ⇒ Object
Install plugins and do every thing that cannot be done in initialize Plugin install will use @view.malline, that will create a duplicate Template instance, if it’s called from initialize.
-
#initialize(view, opts) ⇒ Template
constructor
A new instance of Template.
-
#render(dom = nil) ⇒ Object
Render the XML tree at dom or @dom.
-
#run(tpl = nil, &block) ⇒ Object
Execute and render a text or block.
-
#tag(s, *args, &block) ⇒ Object
Add a tag to @dom.
Constructor Details
#initialize(view, opts) ⇒ Template
Returns a new instance of Template.
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/malline/template.rb', line 40 def initialize view, opts @view = view @whitespace = false @path = 'Malline template' @options = opts @short_tag_excludes = [] @helper_overrides = {} @tags = {} @plugins = [] @inited = false end |
Instance Attribute Details
#helper_overrides ⇒ Object
Every overriden (in definetags!) helper method (:name => method)
32 33 34 |
# File 'lib/malline/template.rb', line 32 def helper_overrides @helper_overrides end |
#options ⇒ Object
Current options (like @@options in Base)
24 25 26 |
# File 'lib/malline/template.rb', line 24 def @options end |
#path ⇒ Object
Current file name
30 31 32 |
# File 'lib/malline/template.rb', line 30 def path @path end |
#plugins ⇒ Object
List all installed plugins
38 39 40 |
# File 'lib/malline/template.rb', line 38 def plugins @plugins end |
#rendered ⇒ Object (readonly)
Render result of the last #render
36 37 38 |
# File 'lib/malline/template.rb', line 36 def rendered @rendered end |
#short_tag_excludes ⇒ Object
List of every tag that doesn’t support self-closing syntax
26 27 28 |
# File 'lib/malline/template.rb', line 26 def short_tag_excludes @short_tag_excludes end |
#tags ⇒ Object
Every available tag, excluding the specific methods (:name => bool)
34 35 36 |
# File 'lib/malline/template.rb', line 34 def @tags end |
#whitespace ⇒ Object
Current state of :whitespace-modifier (bool)
28 29 30 |
# File 'lib/malline/template.rb', line 28 def whitespace @whitespace end |
Class Method Details
.html_escape(s) ⇒ Object
Stolen from ERB, © 1999-2000,2002,2003 Masatoshi SEKI
61 62 63 |
# File 'lib/malline/template.rb', line 61 def self.html_escape(s) s.to_s.gsub(/&/, "&").gsub(/\"/, """).gsub(/>/, ">").gsub(/</, "<") end |
.url_encode(s) ⇒ Object
Stolen from ERB, © 1999-2000,2002,2003 Masatoshi SEKI
65 66 67 |
# File 'lib/malline/template.rb', line 65 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
Add escaped string to @dom
82 83 84 85 |
# File 'lib/malline/template.rb', line 82 def add_text *values @dom << ' ' if @whitespace @dom << Template.html_escape(values.join(' ')) end |
#add_unescaped_text(value) ⇒ Object
Add unescaped string to @dom
88 89 90 91 |
# File 'lib/malline/template.rb', line 88 def add_unescaped_text value @dom << ' ' if @whitespace @dom << value.to_s unless value.nil? end |
#define_tag!(tag) ⇒ Object
Define a method tag
177 178 179 180 181 182 183 |
# File 'lib/malline/template.rb', line 177 def define_tag! tag eval %{ def @view.#{tag}(*args, &block) tag!('#{tag}', *args, &block) end } end |
#definetags(*tags) ⇒ Object
Marking tags as usable, but not overriding anything
172 173 174 |
# File 'lib/malline/template.rb', line 172 def * .flatten.each{|tag| @tags[tag] = true } end |
#definetags!(*tags) ⇒ Object
Define tags as a methods, overriding all same named methods
163 164 165 166 167 168 169 |
# File 'lib/malline/template.rb', line 163 def * .flatten.each do |tag| tag = tag.to_sym @helper_overrides[tag] = @view.method(tag) if @view.respond_to?(tag) define_tag! tag end end |
#execute(dom, tpl = nil, &block) ⇒ Object
Changes dom to active @dom, and executes tpl / block
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/malline/template.rb', line 70 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
Call a helper (a method defined outside malline whose output is stored to @dom)
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/malline/template.rb', line 95 def helper helper, *args, &block helper = helper.to_sym tmp = if h = @helper_overrides[helper] h.call *args, &block else @view.send helper, *args, &block end @dom << ' ' if @whitespace @dom << tmp.to_s tmp end |
#init ⇒ Object
Install plugins and do every thing that cannot be done in initialize Plugin install will use @view.malline, that will create a duplicate Template instance, if it’s called from initialize.
55 56 57 58 |
# File 'lib/malline/template.rb', line 55 def init return if @inited XHTML.install @view if @options[:xhtml] end |
#render(dom = nil) ⇒ Object
Render the XML tree at dom or @dom
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/malline/template.rb', line 130 def render dom = nil @rendered = (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
155 156 157 158 159 160 |
# File 'lib/malline/template.rb', line 155 def run tpl = nil, &block init tmp = [] execute tmp, tpl, &block render tmp end |
#tag(s, *args, &block) ⇒ Object
Add a tag to @dom
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/malline/template.rb', line 108 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 |