123456789_123456789_123456789_123456789_123456789_

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

Instance Method Details

#asciidoc(template, options = {}, locals = {})

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 792

def asciidoc(template, options = {}, locals = {})
  render :asciidoc, template, options, locals
end

#builder(template = nil, options = {}, locals = {}, &block)

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 774

def builder(template = nil, options = {}, locals = {}, &block)
  options[:default_content_type] = :xml
  render_ruby(:builder, template, options, locals, &block)
end

#compile_block_template(template, options, &body) (private)

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 930

def compile_block_template(template, options, &body)
  first_location = caller_locations.first
  path = first_location.path
  line = first_location.lineno
  path = options[:path] || path
  line = options[:line] || line
  template.new(path, line.to_i, options, &body)
end

#compile_template(engine, data, options, views) (private)

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 892

def compile_template(engine, data, options, views)
  eat_errors = options.delete :eat_errors
  template = Tilt[engine]
  raise "Template engine not found: #{engine}" if template.nil?

  case data
  when Symbol
    template_cache.fetch engine, data, options, views do
      body, path, line = settings.templates[data]
      if body
        body = body.call if body.respond_to?(:call)
        template.new(path, line.to_i, options) { 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, options)
      end
    end
  when Proc
    compile_block_template(template, options, &data)
  when String
    template_cache.fetch engine, data, options, views do
      compile_block_template(template, options) { data }
    end
  else
    raise ArgumentError, "Sorry, don't know how to render #{data.inspect}."
  end
end

#erb(template, options = {}, locals = {}, &block)

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 752

def erb(template, options = {}, locals = {}, &block)
  render(:erb, template, options, locals, &block)
end

#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.

Yields:

  • (::File.join(views, "#{name}.#{@preferred_extension}"))
[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 821

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 ]

  
# File 'lib/sinatra/base.rb', line 756

def haml(template, options = {}, locals = {}, &block)
  render(:haml, template, options, locals, &block)
end

#initialize

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 746

def initialize
  super
  @default_layout = :layout
  @preferred_extension = nil
end

#liquid(template, options = {}, locals = {}, &block)

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 779

def liquid(template, options = {}, locals = {}, &block)
  render(:liquid, template, options, locals, &block)
end

#markaby(template = nil, options = {}, locals = {}, &block)

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 796

def markaby(template = nil, options = {}, locals = {}, &block)
  render_ruby(:mab, template, options, locals, &block)
end

#markdown(template, options = {}, locals = {})

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 783

def markdown(template, options = {}, locals = {})
  options[:exclude_outvar] = true
  render :markdown, template, options, locals
end

#nokogiri(template = nil, options = {}, locals = {}, &block)

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 800

def nokogiri(template = nil, options = {}, locals = {}, &block)
  options[:default_content_type] = :xml
  render_ruby(:nokogiri, template, options, locals, &block)
end

#rabl(template, options = {}, locals = {})

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 814

def rabl(template, options = {}, locals = {})
  Rabl.register!
  render :rabl, template, options, locals
end

#rdoc(template, options = {}, locals = {})

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 788

def rdoc(template, options = {}, locals = {})
  render :rdoc, template, options, locals
end

#render(engine, data, options = {}, locals = {}, &block) (private)

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 841

def render(engine, data, options = {}, locals = {}, &block)
  # merge app-level options
  engine_options = settings.respond_to?(engine) ? settings.send(engine) : {}
  options.merge!(engine_options) { |_key, v1, _v2| v1 }

  # extract generic options
  locals          = options.delete(:locals) || locals         || {}
  views           = options.delete(:views)  || settings.views || './views'
  layout          = options[:layout]
  layout          = false if layout.nil? && options.include?(:layout)
  eat_errors      = layout.nil?
  layout          = engine_options[:layout] if layout.nil? || (layout == true && engine_options[:layout] != false)
  layout          = @default_layout         if layout.nil? || (layout == true)
  layout_options  = options.delete(:layout_options) || {}
  content_type    = options.delete(:default_content_type)
  content_type    = options.delete(:content_type)   || content_type
  layout_engine   = options.delete(:layout_engine)  || engine
  scope           = options.delete(:scope)          || self
  exclude_outvar  = options.delete(:exclude_outvar)
  options.delete(:layout)

  # set some defaults
  options[:outvar] ||= '@_out_buf' unless exclude_outvar
  options[:default_encoding] ||= settings.default_encoding

  # compile and render template
  begin
    layout_was      = @default_layout
    @default_layout = false
    template        = compile_template(engine, data, options, views)
    output          = template.render(scope, locals, &block)
  ensure
    @default_layout = layout_was
  end

  # render layout
  if layout
    extra_options = { views: views, layout: false, eat_errors: eat_errors, scope: scope }
    options = options.merge(extra_options).merge!(layout_options)

    catch(:layout_missing) { return render(layout_engine, layout, options, 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

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 832

def render_ruby(engine, template, options = {}, locals = {}, &block)
  if template.is_a?(Hash)
    options = template
    template = nil
  end
  template = proc { block } if template.nil?
  render engine, template, options, locals
end

#sass(template, options = {}, locals = {})

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 760

def sass(template, options = {}, locals = {})
  options[:default_content_type] = :css
  options[:exclude_outvar] = true
  options[:layout] = nil
  render :sass, template, options, locals
end

#scss(template, options = {}, locals = {})

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 767

def scss(template, options = {}, locals = {})
  options[:default_content_type] = :css
  options[:exclude_outvar] = true
  options[:layout] = nil
  render :scss, template, options, locals
end

#slim(template, options = {}, locals = {}, &block)

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 805

def slim(template, options = {}, locals = {}, &block)
  render(:slim, template, options, locals, &block)
end

#yajl(template, options = {}, locals = {})

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 809

def yajl(template, options = {}, locals = {})
  options[:default_content_type] = :json
  render :yajl, template, options, locals
end