Module Sinatra::Templates
In: lib/sinatra/base.rb
Rack::ShowExceptions ShowExceptions Rack::Response Response Base Application\n[lib/sinatra/base.rb\nlib/sinatra/main.rb] Rack::Utils NameError NotFound Rack::Request Request ::File StaticFile lib/sinatra/base.rb lib/sinatra/showexceptions.rb Templates Delegator lib/sinatra/base.rb Helpers Sinatra dot/m_4_0.png

Template rendering methods. Each method takes the name of a template to render as a Symbol and returns a String with the rendered output, as well as an optional hash with additional options.

`template` is either the name or path of the template as symbol (Use `:’subdir/myview’` for views in subdirectories), or a string that will be rendered.

Possible options are:

  :layout       If set to false, no layout is rendered, otherwise
                the specified layout is used (Ignored for `sass`)
  :locals       A hash with local variables that should be available
                in the template

Methods

builder   compile_template   erb   erubis   haml   render   sass  

Public Instance methods

[Source]

     # File lib/sinatra/base.rb, line 308
308:     def builder(template=nil, options={}, locals={}, &block)
309:       options, template = template, nil if template.is_a?(Hash)
310:       template = Proc.new { block } if template.nil?
311:       render :builder, template, options, locals
312:     end

[Source]

     # File lib/sinatra/base.rb, line 291
291:     def erb(template, options={}, locals={})
292:       render :erb, template, options, locals
293:     end

[Source]

     # File lib/sinatra/base.rb, line 295
295:     def erubis(template, options={}, locals={})
296:       render :erubis, template, options, locals
297:     end

[Source]

     # File lib/sinatra/base.rb, line 299
299:     def haml(template, options={}, locals={})
300:       render :haml, template, options, locals
301:     end

[Source]

     # File lib/sinatra/base.rb, line 303
303:     def sass(template, options={}, locals={})
304:       options[:layout] = false
305:       render :sass, template, options, locals
306:     end

Private Instance methods

[Source]

     # File lib/sinatra/base.rb, line 341
341:     def compile_template(engine, data, options, views)
342:       @template_cache.fetch engine, data, options do
343:         case
344:         when data.is_a?(Symbol)
345:           body, path, line = self.class.templates[data]
346:           if body
347:             body = body.call if body.respond_to?(:call)
348:             Tilt[engine].new(path, line.to_i, options) { body }
349:           else
350:             path = ::File.join(views, "#{data}.#{engine}")
351:             Tilt[engine].new(path, 1, options)
352:           end
353:         when data.is_a?(Proc) || data.is_a?(String)
354:           body = data.is_a?(String) ? Proc.new { data } : data
355:           path, line = self.class.caller_locations.first
356:           Tilt[engine].new(path, line.to_i, options, &body)
357:         else
358:           raise ArgumentError
359:         end
360:       end
361:     end

[Source]

     # File lib/sinatra/base.rb, line 315
315:     def render(engine, data, options={}, locals={}, &block)
316:       # merge app-level options
317:       options = settings.send(engine).merge(options) if settings.respond_to?(engine)
318: 
319:       # extract generic options
320:       locals = options.delete(:locals) || locals || {}
321:       views = options.delete(:views) || settings.views || "./views"
322:       layout = options.delete(:layout)
323:       layout = :layout if layout.nil? || layout == true
324: 
325:       # compile and render template
326:       template = compile_template(engine, data, options, views)
327:       output = template.render(self, locals, &block)
328: 
329:       # render layout
330:       if layout
331:         begin
332:           options = options.merge(:views => views, :layout => false)
333:           output = render(engine, layout, options, locals) { output }
334:         rescue Errno::ENOENT
335:         end
336:       end
337: 
338:       output
339:     end

[Validate]