Module Sinatra::Templates

  1. lib/sinatra/base.rb

Public instance methods

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