module Sinatra::Helpers::Stream::Templates

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

Public Class Methods

new() click to toggle source
Calls superclass method
    # File lib/sinatra/base.rb
731 def initialize
732   super
733   @default_layout = :layout
734   @preferred_extension = nil
735 end

Public Instance Methods

asciidoc(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
763 def asciidoc(template, options = {}, locals = {})
764   render :asciidoc, template, options, locals
765 end
builder(template = nil, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
745 def builder(template = nil, options = {}, locals = {}, &block)
746   options[:default_content_type] = :xml
747   render_ruby(:builder, template, options, locals, &block)
748 end
erb(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
737 def erb(template, options = {}, locals = {}, &block)
738   render(:erb, template, options, locals, &block)
739 end
find_template(views, name, engine) { |join(views, "#{name}.#{preferred_extension}")| ... } click to toggle source

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
792 def find_template(views, name, engine)
793   yield ::File.join(views, "#{name}.#{@preferred_extension}")
794 
795   Tilt.default_mapping.extensions_for(engine).each do |ext|
796     yield ::File.join(views, "#{name}.#{ext}") unless ext == @preferred_extension
797   end
798 end
haml(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
741 def haml(template, options = {}, locals = {}, &block)
742   render(:haml, template, options, locals, &block)
743 end
liquid(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
750 def liquid(template, options = {}, locals = {}, &block)
751   render(:liquid, template, options, locals, &block)
752 end
markaby(template = nil, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
767 def markaby(template = nil, options = {}, locals = {}, &block)
768   render_ruby(:mab, template, options, locals, &block)
769 end
markdown(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
754 def markdown(template, options = {}, locals = {})
755   options[:exclude_outvar] = true
756   render :markdown, template, options, locals
757 end
nokogiri(template = nil, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
771 def nokogiri(template = nil, options = {}, locals = {}, &block)
772   options[:default_content_type] = :xml
773   render_ruby(:nokogiri, template, options, locals, &block)
774 end
rabl(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
785 def rabl(template, options = {}, locals = {})
786   Rabl.register!
787   render :rabl, template, options, locals
788 end
rdoc(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
759 def rdoc(template, options = {}, locals = {})
760   render :rdoc, template, options, locals
761 end
slim(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
776 def slim(template, options = {}, locals = {}, &block)
777   render(:slim, template, options, locals, &block)
778 end
yajl(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
780 def yajl(template, options = {}, locals = {})
781   options[:default_content_type] = :json
782   render :yajl, template, options, locals
783 end

Private Instance Methods

compile_block_template(template, options, &body) click to toggle source
    # File lib/sinatra/base.rb
897 def compile_block_template(template, options, &body)
898   first_location = caller_locations.first
899   path = first_location.path
900   line = first_location.lineno
901   path = options[:path] || path
902   line = options[:line] || line
903   template.new(path, line.to_i, options, &body)
904 end
compile_template(engine, data, options, views) click to toggle source
    # File lib/sinatra/base.rb
859 def compile_template(engine, data, options, views)
860   eat_errors = options.delete :eat_errors
861   template = Tilt[engine]
862   raise "Template engine not found: #{engine}" if template.nil?
863 
864   case data
865   when Symbol
866     template_cache.fetch engine, data, options, views do
867       body, path, line = settings.templates[data]
868       if body
869         body = body.call if body.respond_to?(:call)
870         template.new(path, line.to_i, options) { body }
871       else
872         found = false
873         @preferred_extension = engine.to_s
874         find_template(views, data, template) do |file|
875           path ||= file # keep the initial path rather than the last one
876           found = File.exist?(file)
877           if found
878             path = file
879             break
880           end
881         end
882         throw :layout_missing if eat_errors && !found
883         template.new(path, 1, options)
884       end
885     end
886   when Proc
887     compile_block_template(template, options, &data)
888   when String
889     template_cache.fetch engine, data, options, views do
890       compile_block_template(template, options) { data }
891     end
892   else
893     raise ArgumentError, "Sorry, don't know how to render #{data.inspect}."
894   end
895 end
render(engine, data, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
812 def render(engine, data, options = {}, locals = {}, &block)
813   # merge app-level options
814   engine_options = settings.respond_to?(engine) ? settings.send(engine) : {}
815   options.merge!(engine_options) { |_key, v1, _v2| v1 }
816 
817   # extract generic options
818   locals          = options.delete(:locals) || locals         || {}
819   views           = options.delete(:views)  || settings.views || './views'
820   layout          = options[:layout]
821   layout          = false if layout.nil? && options.include?(:layout)
822   eat_errors      = layout.nil?
823   layout          = engine_options[:layout] if layout.nil? || (layout == true && engine_options[:layout] != false)
824   layout          = @default_layout         if layout.nil? || (layout == true)
825   layout_options  = options.delete(:layout_options) || {}
826   content_type    = options.delete(:default_content_type)
827   content_type    = options.delete(:content_type)   || content_type
828   layout_engine   = options.delete(:layout_engine)  || engine
829   scope           = options.delete(:scope)          || self
830   exclude_outvar  = options.delete(:exclude_outvar)
831   options.delete(:layout)
832 
833   # set some defaults
834   options[:outvar] ||= '@_out_buf' unless exclude_outvar
835   options[:default_encoding] ||= settings.default_encoding
836 
837   # compile and render template
838   begin
839     layout_was      = @default_layout
840     @default_layout = false
841     template        = compile_template(engine, data, options, views)
842     output          = template.render(scope, locals, &block)
843   ensure
844     @default_layout = layout_was
845   end
846 
847   # render layout
848   if layout
849     extra_options = { views: views, layout: false, eat_errors: eat_errors, scope: scope }
850     options = options.merge(extra_options).merge!(layout_options)
851 
852     catch(:layout_missing) { return render(layout_engine, layout, options, locals) { output } }
853   end
854 
855   output.extend(ContentTyped).content_type = content_type if content_type
856   output
857 end
render_ruby(engine, template, options = {}, locals = {}, &block) click to toggle source

logic shared between builder and nokogiri

    # File lib/sinatra/base.rb
803 def render_ruby(engine, template, options = {}, locals = {}, &block)
804   if template.is_a?(Hash)
805     options = template
806     template = nil
807   end
808   template = proc { block } if template.nil?
809   render engine, template, options, locals
810 end