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
737 def asciidoc(template, options = {}, locals = {})
738   render :asciidoc, template, options, locals
739 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
749 def coffee(template, options = {}, locals = {})
750   options.merge! :layout => false, :default_content_type => :js
751   render :coffee, template, options, locals
752 end
creole(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
763 def creole(template, options = {}, locals = {})
764   render :creole, template, options, locals
765 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
787 def find_template(views, name, engine)
788   yield ::File.join(views, "#{name}.#{@preferred_extension}")
789 
790   Tilt.default_mapping.extensions_for(engine).each do |ext|
791     yield ::File.join(views, "#{name}.#{ext}") unless ext == @preferred_extension
792   end
793 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
745 def markaby(template = nil, options = {}, locals = {}, &block)
746   render_ruby(:mab, template, options, locals, &block)
747 end
markdown(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
724 def markdown(template, options = {}, locals = {})
725   options[:exclude_outvar] = true
726   render :markdown, template, options, locals
727 end
mediawiki(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
767 def mediawiki(template, options = {}, locals = {})
768   render :mediawiki, template, options, locals
769 end
nokogiri(template = nil, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
754 def nokogiri(template = nil, options = {}, locals = {}, &block)
755   options[:default_content_type] = :xml
756   render_ruby(:nokogiri, template, options, locals, &block)
757 end
rabl(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
780 def rabl(template, options = {}, locals = {})
781   Rabl.register!
782   render :rabl, template, options, locals
783 end
radius(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
741 def radius(template, options = {}, locals = {})
742   render :radius, template, options, locals
743 end
rdoc(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
733 def rdoc(template, options = {}, locals = {})
734   render :rdoc, template, options, locals
735 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
759 def slim(template, options = {}, locals = {}, &block)
760   render(:slim, template, options, locals, &block)
761 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
729 def textile(template, options = {}, locals = {})
730   render :textile, template, options, locals
731 end
wlang(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
771 def wlang(template, options = {}, locals = {}, &block)
772   render(:wlang, template, options, locals, &block)
773 end
yajl(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
775 def yajl(template, options = {}, locals = {})
776   options[:default_content_type] = :json
777   render :yajl, template, options, locals
778 end

Private Instance Methods

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

logic shared between builder and nokogiri

    # File lib/sinatra/base.rb
798 def render_ruby(engine, template, options = {}, locals = {}, &block)
799   options, template = template, nil if template.is_a?(Hash)
800   template = Proc.new { block } if template.nil?
801   render engine, template, options, locals
802 end