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 Tilt::CompileSite ::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` and `less`)
  :locals       A hash with local variables that should be available
                in the template

Methods

builder   compile_template   erb   erubis   haml   less   render   sass  

Included Modules

Tilt::CompileSite

Public Instance methods

[Source]

     # File lib/sinatra/base.rb, line 324
324:     def builder(template=nil, options={}, locals={}, &block)
325:       options, template = template, nil if template.is_a?(Hash)
326:       template = Proc.new { block } if template.nil?
327:       render :builder, template, options, locals
328:     end

[Source]

     # File lib/sinatra/base.rb, line 300
300:     def erb(template, options={}, locals={})
301:       options[:outvar] = '@_out_buf'
302:       render :erb, template, options, locals
303:     end

[Source]

     # File lib/sinatra/base.rb, line 305
305:     def erubis(template, options={}, locals={})
306:       options[:outvar] = '@_out_buf'
307:       render :erubis, template, options, locals
308:     end

[Source]

     # File lib/sinatra/base.rb, line 310
310:     def haml(template, options={}, locals={})
311:       render :haml, template, options, locals
312:     end

[Source]

     # File lib/sinatra/base.rb, line 319
319:     def less(template, options={}, locals={})
320:       options[:layout] = false
321:       render :less, template, options, locals
322:     end

[Source]

     # File lib/sinatra/base.rb, line 314
314:     def sass(template, options={}, locals={})
315:       options[:layout] = false
316:       render :sass, template, options, locals
317:     end

Private Instance methods

[Source]

     # File lib/sinatra/base.rb, line 357
357:     def compile_template(engine, data, options, views)
358:       @template_cache.fetch engine, data, options do
359:         template = Tilt[engine]
360:         raise "Template engine not found: #{engine}" if template.nil?
361: 
362:         case
363:         when data.is_a?(Symbol)
364:           body, path, line = self.class.templates[data]
365:           if body
366:             body = body.call if body.respond_to?(:call)
367:             template.new(path, line.to_i, options) { body }
368:           else
369:             path = ::File.join(views, "#{data}.#{engine}")
370:             template.new(path, 1, options)
371:           end
372:         when data.is_a?(Proc) || data.is_a?(String)
373:           body = data.is_a?(String) ? Proc.new { data } : data
374:           path, line = self.class.caller_locations.first
375:           template.new(path, line.to_i, options, &body)
376:         else
377:           raise ArgumentError
378:         end
379:       end
380:     end

[Source]

     # File lib/sinatra/base.rb, line 331
331:     def render(engine, data, options={}, locals={}, &block)
332:       # merge app-level options
333:       options = settings.send(engine).merge(options) if settings.respond_to?(engine)
334: 
335:       # extract generic options
336:       locals = options.delete(:locals) || locals || {}
337:       views = options.delete(:views) || settings.views || "./views"
338:       layout = options.delete(:layout)
339:       layout = :layout if layout.nil? || layout == true
340: 
341:       # compile and render template
342:       template = compile_template(engine, data, options, views)
343:       output = template.render(self, locals, &block)
344: 
345:       # render layout
346:       if layout
347:         begin
348:           options = options.merge(:views => views, :layout => false)
349:           output = render(engine, layout, options, locals) { output }
350:         rescue Errno::ENOENT
351:         end
352:       end
353: 
354:       output
355:     end

[Validate]