Module: Sinatra::Templates
- Included in:
- Base
- Defined in:
- lib/sinatra/base.rb
Instance Method Summary collapse
- #builder(template = nil, options = {}, &block) ⇒ Object
- #erb(template, options = {}) ⇒ Object
- #haml(template, options = {}) ⇒ Object
- #lookup_layout(engine, options) ⇒ Object
- #lookup_template(engine, template, options = {}) ⇒ Object
- #render(engine, template, options = {}) ⇒ Object
- #render_builder(template, data, options, &block) ⇒ Object
- #render_erb(template, data, options, &block) ⇒ Object
- #render_haml(template, data, options, &block) ⇒ Object
- #render_sass(template, data, options, &block) ⇒ Object
- #sass(template, options = {}, &block) ⇒ Object
- #template_path(engine, template, options = {}) ⇒ Object
Instance Method Details
#builder(template = nil, options = {}, &block) ⇒ Object
271 272 273 274 275 276 |
# File 'lib/sinatra/base.rb', line 271 def builder(template=nil, ={}, &block) require 'builder' unless defined? ::Builder , template = template, nil if template.is_a?(Hash) template = lambda { block } if template.nil? render :builder, template, end |
#erb(template, options = {}) ⇒ Object
234 235 236 237 |
# File 'lib/sinatra/base.rb', line 234 def erb(template, ={}) require 'erb' unless defined? ::ERB render :erb, template, end |
#haml(template, options = {}) ⇒ Object
249 250 251 252 253 |
# File 'lib/sinatra/base.rb', line 249 def haml(template, ={}) require 'haml' unless defined? ::Haml [:options] ||= self.class.haml if self.class.respond_to? :haml render :haml, template, end |
#lookup_layout(engine, options) ⇒ Object
218 219 220 221 222 223 224 225 226 |
# File 'lib/sinatra/base.rb', line 218 def lookup_layout(engine, ) return if [:layout] == false .delete(:layout) if [:layout] == true template = [:layout] || :layout data = lookup_template(engine, template, ) [template, data] rescue Errno::ENOENT nil end |
#lookup_template(engine, template, options = {}) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/sinatra/base.rb', line 201 def lookup_template(engine, template, ={}) case template when Symbol if cached = self.class.templates[template] lookup_template(engine, cached, ) else ::File.read(template_path(engine, template, )) end when Proc template.call when String template else raise ArgumentError end end |
#render(engine, template, options = {}) ⇒ Object
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/sinatra/base.rb', line 190 def render(engine, template, ={}) data = lookup_template(engine, template, ) output = __send__("render_#{engine}", template, data, ) layout, data = lookup_layout(engine, ) if layout __send__("render_#{engine}", layout, data, ) { output } else output end end |
#render_builder(template, data, options, &block) ⇒ Object
278 279 280 281 282 283 284 285 286 |
# File 'lib/sinatra/base.rb', line 278 def render_builder(template, data, , &block) xml = ::Builder::XmlMarkup.new(:indent => 2) if data.respond_to?(:to_str) eval data.to_str, binding, '<BUILDER>', 1 elsif data.kind_of?(Proc) data.call(xml) end xml.target! end |
#render_erb(template, data, options, &block) ⇒ Object
239 240 241 242 243 244 245 246 247 |
# File 'lib/sinatra/base.rb', line 239 def render_erb(template, data, , &block) data = data.call if data.kind_of? Proc instance = ::ERB.new(data) locals = [:locals] || {} locals_assigns = locals.to_a.collect { |k,v| "#{k} = locals[:#{k}]" } src = "#{locals_assigns.join("\n")}\n#{instance.src}" eval src, binding, '(__ERB__)', locals_assigns.length + 1 instance.result(binding) end |
#render_haml(template, data, options, &block) ⇒ Object
255 256 257 258 |
# File 'lib/sinatra/base.rb', line 255 def render_haml(template, data, , &block) engine = ::Haml::Engine.new(data, [:options] || {}) engine.render(self, [:locals] || {}, &block) end |
#render_sass(template, data, options, &block) ⇒ Object
266 267 268 269 |
# File 'lib/sinatra/base.rb', line 266 def render_sass(template, data, , &block) engine = ::Sass::Engine.new(data, [:sass] || {}) engine.render end |
#sass(template, options = {}, &block) ⇒ Object
260 261 262 263 264 |
# File 'lib/sinatra/base.rb', line 260 def sass(template, ={}, &block) require 'sass' unless defined? ::Sass [:layout] = false render :sass, template, end |
#template_path(engine, template, options = {}) ⇒ Object
228 229 230 231 232 |
# File 'lib/sinatra/base.rb', line 228 def template_path(engine, template, ={}) views_dir = [:views_directory] || self..views || "./views" "#{views_dir}/#{template}.#{engine}" end |