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 (Ignored for `sass` and `less`)
: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
682 def initialize
683   super
684   @default_layout = :layout
685   @preferred_extension = nil
686 end

Public Instance Methods

asciidoc(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
744 def asciidoc(template, options = {}, locals = {})
745   render :asciidoc, template, options, locals
746 end
builder(template = nil, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
722 def builder(template = nil, options = {}, locals = {}, &block)
723   options[:default_content_type] = :xml
724   render_ruby(:builder, template, options, locals, &block)
725 end
coffee(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
756 def coffee(template, options = {}, locals = {})
757   options.merge! :layout => false, :default_content_type => :js
758   render :coffee, template, options, locals
759 end
creole(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
770 def creole(template, options = {}, locals = {})
771   render :creole, template, options, locals
772 end
erb(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
688 def erb(template, options = {}, locals = {}, &block)
689   render(:erb, template, options, locals, &block)
690 end
erubis(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
692 def erubis(template, options = {}, locals = {})
693   warn "Sinatra::Templates#erubis is deprecated and will be removed, use #erb instead.\n" \
694     "If you have Erubis installed, it will be used automatically."
695   render :erubis, template, options, locals
696 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
794 def find_template(views, name, engine)
795   yield ::File.join(views, "#{name}.#{@preferred_extension}")
796 
797   Tilt.default_mapping.extensions_for(engine).each do |ext|
798     yield ::File.join(views, "#{name}.#{ext}") unless ext == @preferred_extension
799   end
800 end
haml(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
698 def haml(template, options = {}, locals = {}, &block)
699   render(:haml, template, options, locals, &block)
700 end
less(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
712 def less(template, options = {}, locals = {})
713   options.merge! :layout => false, :default_content_type => :css
714   render :less, template, options, locals
715 end
liquid(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
727 def liquid(template, options = {}, locals = {}, &block)
728   render(:liquid, template, options, locals, &block)
729 end
markaby(template = nil, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
752 def markaby(template = nil, options = {}, locals = {}, &block)
753   render_ruby(:mab, template, options, locals, &block)
754 end
markdown(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
731 def markdown(template, options = {}, locals = {})
732   options[:exclude_outvar] = true
733   render :markdown, template, options, locals
734 end
mediawiki(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
774 def mediawiki(template, options = {}, locals = {})
775   render :mediawiki, template, options, locals
776 end
nokogiri(template = nil, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
761 def nokogiri(template = nil, options = {}, locals = {}, &block)
762   options[:default_content_type] = :xml
763   render_ruby(:nokogiri, template, options, locals, &block)
764 end
rabl(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
787 def rabl(template, options = {}, locals = {})
788   Rabl.register!
789   render :rabl, template, options, locals
790 end
radius(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
748 def radius(template, options = {}, locals = {})
749   render :radius, template, options, locals
750 end
rdoc(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
740 def rdoc(template, options = {}, locals = {})
741   render :rdoc, template, options, locals
742 end
sass(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
702 def sass(template, options = {}, locals = {})
703   options.merge! :layout => false, :default_content_type => :css
704   render :sass, template, options, locals
705 end
scss(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
707 def scss(template, options = {}, locals = {})
708   options.merge! :layout => false, :default_content_type => :css
709   render :scss, template, options, locals
710 end
slim(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
766 def slim(template, options = {}, locals = {}, &block)
767   render(:slim, template, options, locals, &block)
768 end
stylus(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
717 def stylus(template, options = {}, locals = {})
718   options.merge! :layout => false, :default_content_type => :css
719   render :styl, template, options, locals
720 end
textile(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
736 def textile(template, options = {}, locals = {})
737   render :textile, template, options, locals
738 end
wlang(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
778 def wlang(template, options = {}, locals = {}, &block)
779   render(:wlang, template, options, locals, &block)
780 end
yajl(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
782 def yajl(template, options = {}, locals = {})
783   options[:default_content_type] = :json
784   render :yajl, template, options, locals
785 end

Private Instance Methods

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

logic shared between builder and nokogiri

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