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