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
675 def initialize
676   super
677   @default_layout = :layout
678   @preferred_extension = nil
679 end

Public Instance Methods

asciidoc(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
736 def asciidoc(template, options = {}, locals = {})
737   render :asciidoc, template, options, locals
738 end
builder(template = nil, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
715 def builder(template = nil, options = {}, locals = {}, &block)
716   options[:default_content_type] = :xml
717   render_ruby(:builder, template, options, locals, &block)
718 end
coffee(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
748 def coffee(template, options = {}, locals = {})
749   options.merge! :layout => false, :default_content_type => :js
750   render :coffee, template, options, locals
751 end
creole(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
762 def creole(template, options = {}, locals = {})
763   render :creole, template, options, locals
764 end
erb(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
681 def erb(template, options = {}, locals = {}, &block)
682   render(:erb, template, options, locals, &block)
683 end
erubis(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
685 def erubis(template, options = {}, locals = {})
686   warn "Sinatra::Templates#erubis is deprecated and will be removed, use #erb instead.\n" \
687     "If you have Erubis installed, it will be used automatically."
688   render :erubis, template, options, locals
689 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
786 def find_template(views, name, engine)
787   yield ::File.join(views, "#{name}.#{@preferred_extension}")
788 
789   if Tilt.respond_to?(:mappings)
790     Tilt.mappings.each do |ext, engines|
791       next unless ext != @preferred_extension and engines.include? engine
792       yield ::File.join(views, "#{name}.#{ext}")
793     end
794   else
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
799 end
haml(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
691 def haml(template, options = {}, locals = {}, &block)
692   render(:haml, template, options, locals, &block)
693 end
less(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
705 def less(template, options = {}, locals = {})
706   options.merge! :layout => false, :default_content_type => :css
707   render :less, template, options, locals
708 end
liquid(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
720 def liquid(template, options = {}, locals = {}, &block)
721   render(:liquid, template, options, locals, &block)
722 end
markaby(template = nil, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
744 def markaby(template = nil, options = {}, locals = {}, &block)
745   render_ruby(:mab, template, options, locals, &block)
746 end
markdown(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
724 def markdown(template, options = {}, locals = {})
725   render :markdown, template, options, locals
726 end
mediawiki(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
766 def mediawiki(template, options = {}, locals = {})
767   render :mediawiki, template, options, locals
768 end
nokogiri(template = nil, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
753 def nokogiri(template = nil, options = {}, locals = {}, &block)
754   options[:default_content_type] = :xml
755   render_ruby(:nokogiri, template, options, locals, &block)
756 end
rabl(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
779 def rabl(template, options = {}, locals = {})
780   Rabl.register!
781   render :rabl, template, options, locals
782 end
radius(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
740 def radius(template, options = {}, locals = {})
741   render :radius, template, options, locals
742 end
rdoc(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
732 def rdoc(template, options = {}, locals = {})
733   render :rdoc, template, options, locals
734 end
sass(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
695 def sass(template, options = {}, locals = {})
696   options.merge! :layout => false, :default_content_type => :css
697   render :sass, template, options, locals
698 end
scss(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
700 def scss(template, options = {}, locals = {})
701   options.merge! :layout => false, :default_content_type => :css
702   render :scss, template, options, locals
703 end
slim(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
758 def slim(template, options = {}, locals = {}, &block)
759   render(:slim, template, options, locals, &block)
760 end
stylus(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
710 def stylus(template, options = {}, locals = {})
711   options.merge! :layout => false, :default_content_type => :css
712   render :styl, template, options, locals
713 end
textile(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
728 def textile(template, options = {}, locals = {})
729   render :textile, template, options, locals
730 end
wlang(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
770 def wlang(template, options = {}, locals = {}, &block)
771   render(:wlang, template, options, locals, &block)
772 end
yajl(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
774 def yajl(template, options = {}, locals = {})
775   options[:default_content_type] = :json
776   render :yajl, template, options, locals
777 end

Private Instance Methods

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

logic shared between builder and nokogiri

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