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
719 def initialize
720   super
721   @default_layout = :layout
722   @preferred_extension = nil
723 end

Public Instance Methods

asciidoc(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
781 def asciidoc(template, options = {}, locals = {})
782   render :asciidoc, template, options, locals
783 end
builder(template = nil, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
759 def builder(template = nil, options = {}, locals = {}, &block)
760   options[:default_content_type] = :xml
761   render_ruby(:builder, template, options, locals, &block)
762 end
coffee(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
793 def coffee(template, options = {}, locals = {})
794   options.merge! :layout => false, :default_content_type => :js
795   render :coffee, template, options, locals
796 end
creole(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
807 def creole(template, options = {}, locals = {})
808   render :creole, template, options, locals
809 end
erb(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
725 def erb(template, options = {}, locals = {}, &block)
726   render(:erb, template, options, locals, &block)
727 end
erubis(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
729 def erubis(template, options = {}, locals = {})
730   warn "Sinatra::Templates#erubis is deprecated and will be removed, use #erb instead.\n" \
731     "If you have Erubis installed, it will be used automatically."
732   render :erubis, template, options, locals
733 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
831 def find_template(views, name, engine)
832   yield ::File.join(views, "#{name}.#{@preferred_extension}")
833 
834   Tilt.default_mapping.extensions_for(engine).each do |ext|
835     yield ::File.join(views, "#{name}.#{ext}") unless ext == @preferred_extension
836   end
837 end
haml(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
735 def haml(template, options = {}, locals = {}, &block)
736   render(:haml, template, options, locals, &block)
737 end
less(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
749 def less(template, options = {}, locals = {})
750   options.merge! :layout => false, :default_content_type => :css
751   render :less, template, options, locals
752 end
liquid(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
764 def liquid(template, options = {}, locals = {}, &block)
765   render(:liquid, template, options, locals, &block)
766 end
markaby(template = nil, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
789 def markaby(template = nil, options = {}, locals = {}, &block)
790   render_ruby(:mab, template, options, locals, &block)
791 end
markdown(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
768 def markdown(template, options = {}, locals = {})
769   options[:exclude_outvar] = true
770   render :markdown, template, options, locals
771 end
mediawiki(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
811 def mediawiki(template, options = {}, locals = {})
812   render :mediawiki, template, options, locals
813 end
nokogiri(template = nil, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
798 def nokogiri(template = nil, options = {}, locals = {}, &block)
799   options[:default_content_type] = :xml
800   render_ruby(:nokogiri, template, options, locals, &block)
801 end
rabl(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
824 def rabl(template, options = {}, locals = {})
825   Rabl.register!
826   render :rabl, template, options, locals
827 end
radius(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
785 def radius(template, options = {}, locals = {})
786   render :radius, template, options, locals
787 end
rdoc(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
777 def rdoc(template, options = {}, locals = {})
778   render :rdoc, template, options, locals
779 end
sass(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
739 def sass(template, options = {}, locals = {})
740   options.merge! :layout => false, :default_content_type => :css
741   render :sass, template, options, locals
742 end
scss(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
744 def scss(template, options = {}, locals = {})
745   options.merge! :layout => false, :default_content_type => :css
746   render :scss, template, options, locals
747 end
slim(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
803 def slim(template, options = {}, locals = {}, &block)
804   render(:slim, template, options, locals, &block)
805 end
stylus(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
754 def stylus(template, options = {}, locals = {})
755   options.merge! :layout => false, :default_content_type => :css
756   render :styl, template, options, locals
757 end
textile(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
773 def textile(template, options = {}, locals = {})
774   render :textile, template, options, locals
775 end
wlang(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
815 def wlang(template, options = {}, locals = {}, &block)
816   render(:wlang, template, options, locals, &block)
817 end
yajl(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
819 def yajl(template, options = {}, locals = {})
820   options[:default_content_type] = :json
821   render :yajl, template, options, locals
822 end

Private Instance Methods

compile_block_template(template, options, &body) click to toggle source
    # File lib/sinatra/base.rb
931 def compile_block_template(template, options, &body)
932   caller = settings.caller_locations.first
933   path = options[:path] || caller[0]
934   line = options[:line] || caller[1]
935   template.new(path, line.to_i, options, &body)
936 end
compile_template(engine, data, options, views) click to toggle source
    # File lib/sinatra/base.rb
894 def compile_template(engine, data, options, views)
895   eat_errors = options.delete :eat_errors
896   template = Tilt[engine]
897   raise "Template engine not found: #{engine}" if template.nil?
898 
899   case data
900   when Symbol
901     template_cache.fetch engine, data, options, views do
902       body, path, line = settings.templates[data]
903       if body
904         body = body.call if body.respond_to?(:call)
905         template.new(path, line.to_i, options) { body }
906       else
907         found = false
908         @preferred_extension = engine.to_s
909         find_template(views, data, template) do |file|
910           path ||= file # keep the initial path rather than the last one
911           if found = File.exist?(file)
912             path = file
913             break
914           end
915         end
916         throw :layout_missing if eat_errors and not found
917         template.new(path, 1, options)
918       end
919     end
920   when Proc
921     compile_block_template(template, options, &data)
922   when String
923     template_cache.fetch engine, data, options, views do
924       compile_block_template(template, options) { data }
925     end
926   else
927     raise ArgumentError, "Sorry, don't know how to render #{data.inspect}."
928   end
929 end
render(engine, data, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
848 def render(engine, data, options = {}, locals = {}, &block)
849   # merge app-level options
850   engine_options = settings.respond_to?(engine) ? settings.send(engine) : {}
851   options.merge!(engine_options) { |key, v1, v2| v1 }
852 
853   # extract generic options
854   locals          = options.delete(:locals) || locals         || {}
855   views           = options.delete(:views)  || settings.views || "./views"
856   layout          = options[:layout]
857   layout          = false if layout.nil? && options.include?(:layout)
858   eat_errors      = layout.nil?
859   layout          = engine_options[:layout] if layout.nil? or (layout == true && engine_options[:layout] != false)
860   layout          = @default_layout         if layout.nil? or layout == true
861   layout_options  = options.delete(:layout_options) || {}
862   content_type    = options.delete(:default_content_type)
863   content_type    = options.delete(:content_type)   || content_type
864   layout_engine   = options.delete(:layout_engine)  || engine
865   scope           = options.delete(:scope)          || self
866   exclude_outvar  = options.delete(:exclude_outvar)
867   options.delete(:layout)
868 
869   # set some defaults
870   options[:outvar] ||= '@_out_buf' unless exclude_outvar
871   options[:default_encoding] ||= settings.default_encoding
872 
873   # compile and render template
874   begin
875     layout_was      = @default_layout
876     @default_layout = false
877     template        = compile_template(engine, data, options, views)
878     output          = template.render(scope, locals, &block)
879   ensure
880     @default_layout = layout_was
881   end
882 
883   # render layout
884   if layout
885     options = options.merge(:views => views, :layout => false, :eat_errors => eat_errors, :scope => scope).
886             merge!(layout_options)
887     catch(:layout_missing) { return render(layout_engine, layout, options, locals) { output } }
888   end
889 
890   output.extend(ContentTyped).content_type = content_type if content_type
891   output
892 end
render_ruby(engine, template, options = {}, locals = {}, &block) click to toggle source

logic shared between builder and nokogiri

    # File lib/sinatra/base.rb
842 def render_ruby(engine, template, options = {}, locals = {}, &block)
843   options, template = template, nil if template.is_a?(Hash)
844   template = Proc.new { block } if template.nil?
845   render engine, template, options, locals
846 end