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
697 def initialize
698   super
699   @default_layout = :layout
700   @preferred_extension = nil
701 end

Public Instance Methods

asciidoc(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
759 def asciidoc(template, options = {}, locals = {})
760   render :asciidoc, template, options, locals
761 end
builder(template = nil, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
737 def builder(template = nil, options = {}, locals = {}, &block)
738   options[:default_content_type] = :xml
739   render_ruby(:builder, template, options, locals, &block)
740 end
coffee(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
771 def coffee(template, options = {}, locals = {})
772   options.merge! :layout => false, :default_content_type => :js
773   render :coffee, template, options, locals
774 end
creole(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
785 def creole(template, options = {}, locals = {})
786   render :creole, template, options, locals
787 end
erb(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
703 def erb(template, options = {}, locals = {}, &block)
704   render(:erb, template, options, locals, &block)
705 end
erubis(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
707 def erubis(template, options = {}, locals = {})
708   warn "Sinatra::Templates#erubis is deprecated and will be removed, use #erb instead.\n" \
709     "If you have Erubis installed, it will be used automatically."
710   render :erubis, template, options, locals
711 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
809 def find_template(views, name, engine)
810   yield ::File.join(views, "#{name}.#{@preferred_extension}")
811 
812   Tilt.default_mapping.extensions_for(engine).each do |ext|
813     yield ::File.join(views, "#{name}.#{ext}") unless ext == @preferred_extension
814   end
815 end
haml(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
713 def haml(template, options = {}, locals = {}, &block)
714   render(:haml, template, options, locals, &block)
715 end
less(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
727 def less(template, options = {}, locals = {})
728   options.merge! :layout => false, :default_content_type => :css
729   render :less, template, options, locals
730 end
liquid(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
742 def liquid(template, options = {}, locals = {}, &block)
743   render(:liquid, template, options, locals, &block)
744 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
746 def markdown(template, options = {}, locals = {})
747   options[:exclude_outvar] = true
748   render :markdown, template, options, locals
749 end
mediawiki(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
789 def mediawiki(template, options = {}, locals = {})
790   render :mediawiki, template, options, locals
791 end
nokogiri(template = nil, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
776 def nokogiri(template = nil, options = {}, locals = {}, &block)
777   options[:default_content_type] = :xml
778   render_ruby(:nokogiri, template, options, locals, &block)
779 end
rabl(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
802 def rabl(template, options = {}, locals = {})
803   Rabl.register!
804   render :rabl, template, options, locals
805 end
radius(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
763 def radius(template, options = {}, locals = {})
764   render :radius, template, options, locals
765 end
rdoc(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
755 def rdoc(template, options = {}, locals = {})
756   render :rdoc, template, options, locals
757 end
sass(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
717 def sass(template, options = {}, locals = {})
718   options.merge! :layout => false, :default_content_type => :css
719   render :sass, template, options, locals
720 end
scss(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
722 def scss(template, options = {}, locals = {})
723   options.merge! :layout => false, :default_content_type => :css
724   render :scss, template, options, locals
725 end
slim(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
781 def slim(template, options = {}, locals = {}, &block)
782   render(:slim, template, options, locals, &block)
783 end
stylus(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
732 def stylus(template, options = {}, locals = {})
733   options.merge! :layout => false, :default_content_type => :css
734   render :styl, template, options, locals
735 end
textile(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
751 def textile(template, options = {}, locals = {})
752   render :textile, template, options, locals
753 end
wlang(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
793 def wlang(template, options = {}, locals = {}, &block)
794   render(:wlang, template, options, locals, &block)
795 end
yajl(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
797 def yajl(template, options = {}, locals = {})
798   options[:default_content_type] = :json
799   render :yajl, template, options, locals
800 end

Private Instance Methods

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

logic shared between builder and nokogiri

    # File lib/sinatra/base.rb
820 def render_ruby(engine, template, options = {}, locals = {}, &block)
821   options, template = template, nil if template.is_a?(Hash)
822   template = Proc.new { block } if template.nil?
823   render engine, template, options, locals
824 end