Module: Sinatra::Templates
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Defined in: | lib/sinatra/base.rb |
Overview
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: :content_type The content type to use, same arguments as content_type. :layout If set to something falsy, no layout is rendered, otherwise the specified layout is used (Ignored for #sass) :layout_engine Engine to use for rendering the layout. :locals A hash with local variables that should be available in the template :scope If set, template is evaluate with the binding of the given object rather than the application instance. :views Views directory to use.
Instance Method Summary
- #asciidoc(template, options = {}, locals = {})
- #builder(template = nil, options = {}, locals = {}, &block)
- #erb(template, options = {}, locals = {}, &block)
-
#find_template(views, name, engine) {|::File.join(views, "#{name}.#{@preferred_extension}")| ... }
Calls the given block for every possible template file in views, named name.ext, where ext is registered on engine.
- #haml(template, options = {}, locals = {}, &block)
- #initialize
- #liquid(template, options = {}, locals = {}, &block)
- #markaby(template = nil, options = {}, locals = {}, &block)
- #markdown(template, options = {}, locals = {})
- #nokogiri(template = nil, options = {}, locals = {}, &block)
- #rabl(template, options = {}, locals = {})
- #rdoc(template, options = {}, locals = {})
- #sass(template, options = {}, locals = {})
- #scss(template, options = {}, locals = {})
- #slim(template, options = {}, locals = {}, &block)
- #yajl(template, options = {}, locals = {})
- #compile_block_template(template, options, &body) private
- #compile_template(engine, data, options, views) private
- #render(engine, data, options = {}, locals = {}, &block) private
-
#render_ruby(engine, template, options = {}, locals = {}, &block)
private
logic shared between builder and nokogiri.
Instance Method Details
#asciidoc(template, options = {}, locals = {})
[ GitHub ]#builder(template = nil, options = {}, locals = {}, &block)
[ GitHub ]# File 'lib/sinatra/base.rb', line 777
def builder(template = nil, = {}, locals = {}, &block) [:default_content_type] = :xml render_ruby(:builder, template, , locals, &block) end
#compile_block_template(template, options, &body) (private)
[ GitHub ]#compile_template(engine, data, options, views) (private)
[ GitHub ]# File 'lib/sinatra/base.rb', line 895
def compile_template(engine, data, , views) eat_errors = .delete :eat_errors template = Tilt[engine] raise "Template engine not found: #{engine}" if template.nil? case data when Symbol template_cache.fetch engine, data, , views do body, path, line = settings.templates[data] if body body = body.call if body.respond_to?(:call) template.new(path, line.to_i, ) { body } else found = false @preferred_extension = engine.to_s find_template(views, data, template) do |file| path ||= file # keep the initial path rather than the last one found = File.exist?(file) if found path = file break end end throw :layout_missing if eat_errors && !found template.new(path, 1, ) end end when Proc compile_block_template(template, , &data) when String template_cache.fetch engine, data, , views do compile_block_template(template, ) { data } end else raise ArgumentError, "Sorry, don't know how to render #{data.inspect}." end end
#erb(template, options = {}, locals = {}, &block)
[ GitHub ]#find_template(views, name, engine) {|::File.join(views, "#{name}.#{@preferred_extension}")| ... }
Calls the given block for every possible template file in views, named name.ext, where ext is registered on engine.
# File 'lib/sinatra/base.rb', line 824
def find_template(views, name, engine) yield ::File.join(views, "#{name}.#{@preferred_extension}") Tilt.default_mapping.extensions_for(engine).each do |ext| yield ::File.join(views, "#{name}.#{ext}") unless ext == @preferred_extension end end
#haml(template, options = {}, locals = {}, &block)
[ GitHub ]#initialize
[ GitHub ]# File 'lib/sinatra/base.rb', line 749
def initialize super @default_layout = :layout @preferred_extension = nil end
#liquid(template, options = {}, locals = {}, &block)
[ GitHub ]#markaby(template = nil, options = {}, locals = {}, &block)
[ GitHub ]# File 'lib/sinatra/base.rb', line 799
def markaby(template = nil, = {}, locals = {}, &block) render_ruby(:mab, template, , locals, &block) end
#markdown(template, options = {}, locals = {})
[ GitHub ]#nokogiri(template = nil, options = {}, locals = {}, &block)
[ GitHub ]# File 'lib/sinatra/base.rb', line 803
def nokogiri(template = nil, = {}, locals = {}, &block) [:default_content_type] = :xml render_ruby(:nokogiri, template, , locals, &block) end
#rabl(template, options = {}, locals = {})
[ GitHub ]#rdoc(template, options = {}, locals = {})
[ GitHub ]#render(engine, data, options = {}, locals = {}, &block) (private)
[ GitHub ]# File 'lib/sinatra/base.rb', line 844
def render(engine, data, = {}, locals = {}, &block) # merge app-level options = settings.respond_to?(engine) ? settings.send(engine) : {} .merge!( ) { |_key, v1, _v2| v1 } # extract generic options locals = .delete(:locals) || locals || {} views = .delete(:views) || settings.views || './views' layout = [:layout] layout = false if layout.nil? && .include?(:layout) eat_errors = layout.nil? layout = [:layout] if layout.nil? || (layout == true && [:layout] != false) layout = @default_layout if layout.nil? || (layout == true) = .delete(: ) || {} content_type = .delete(:default_content_type) content_type = .delete(:content_type) || content_type layout_engine = .delete(:layout_engine) || engine scope = .delete(:scope) || self exclude_outvar = .delete(:exclude_outvar) .delete(:layout) # set some defaults [:outvar] ||= '@_out_buf' unless exclude_outvar [:default_encoding] ||= settings.default_encoding # compile and render template begin layout_was = @default_layout @default_layout = false template = compile_template(engine, data, , views) output = template.render(scope, locals, &block) ensure @default_layout = layout_was end # render layout if layout = { views: views, layout: false, eat_errors: eat_errors, scope: scope } = .merge( ).merge!( ) catch(:layout_missing) { return render(layout_engine, layout, , locals) { output } } end if content_type # sass-embedded returns a frozen string output = +output output.extend(ContentTyped).content_type = content_type end output end
#render_ruby(engine, template, options = {}, locals = {}, &block) (private)
logic shared between builder and nokogiri