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
658 def initialize
659   super
660   @default_layout = :layout
661   @preferred_extension = nil
662 end

Public Instance Methods

asciidoc(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
719 def asciidoc(template, options = {}, locals = {})
720   render :asciidoc, template, options, locals
721 end
builder(template = nil, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
698 def builder(template = nil, options = {}, locals = {}, &block)
699   options[:default_content_type] = :xml
700   render_ruby(:builder, template, options, locals, &block)
701 end
coffee(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
731 def coffee(template, options = {}, locals = {})
732   options.merge! :layout => false, :default_content_type => :js
733   render :coffee, template, options, locals
734 end
creole(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
745 def creole(template, options = {}, locals = {})
746   render :creole, template, options, locals
747 end
erb(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
664 def erb(template, options = {}, locals = {}, &block)
665   render(:erb, template, options, locals, &block)
666 end
erubis(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
668 def erubis(template, options = {}, locals = {})
669   warn "Sinatra::Templates#erubis is deprecated and will be removed, use #erb instead.\n" \
670     "If you have Erubis installed, it will be used automatically."
671   render :erubis, template, options, locals
672 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
769 def find_template(views, name, engine)
770   yield ::File.join(views, "#{name}.#{@preferred_extension}")
771   Tilt.mappings.each do |ext, engines|
772     next unless ext != @preferred_extension and engines.include? engine
773     yield ::File.join(views, "#{name}.#{ext}")
774   end
775 end
haml(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
674 def haml(template, options = {}, locals = {}, &block)
675   render(:haml, template, options, locals, &block)
676 end
less(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
688 def less(template, options = {}, locals = {})
689   options.merge! :layout => false, :default_content_type => :css
690   render :less, template, options, locals
691 end
liquid(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
703 def liquid(template, options = {}, locals = {}, &block)
704   render(:liquid, template, options, locals, &block)
705 end
markaby(template = nil, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
727 def markaby(template = nil, options = {}, locals = {}, &block)
728   render_ruby(:mab, template, options, locals, &block)
729 end
markdown(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
707 def markdown(template, options = {}, locals = {})
708   render :markdown, template, options, locals
709 end
mediawiki(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
749 def mediawiki(template, options = {}, locals = {})
750   render :mediawiki, template, options, locals
751 end
nokogiri(template = nil, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
736 def nokogiri(template = nil, options = {}, locals = {}, &block)
737   options[:default_content_type] = :xml
738   render_ruby(:nokogiri, template, options, locals, &block)
739 end
rabl(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
762 def rabl(template, options = {}, locals = {})
763   Rabl.register!
764   render :rabl, template, options, locals
765 end
radius(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
723 def radius(template, options = {}, locals = {})
724   render :radius, template, options, locals
725 end
rdoc(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
715 def rdoc(template, options = {}, locals = {})
716   render :rdoc, template, options, locals
717 end
sass(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
678 def sass(template, options = {}, locals = {})
679   options.merge! :layout => false, :default_content_type => :css
680   render :sass, template, options, locals
681 end
scss(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
683 def scss(template, options = {}, locals = {})
684   options.merge! :layout => false, :default_content_type => :css
685   render :scss, template, options, locals
686 end
slim(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
741 def slim(template, options = {}, locals = {}, &block)
742   render(:slim, template, options, locals, &block)
743 end
stylus(template, options={}, locals={}) click to toggle source
    # File lib/sinatra/base.rb
693 def stylus(template, options={}, locals={})
694   options.merge! :layout => false, :default_content_type => :css
695   render :styl, template, options, locals
696 end
textile(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
711 def textile(template, options = {}, locals = {})
712   render :textile, template, options, locals
713 end
wlang(template, options = {}, locals = {}, &block) click to toggle source
    # File lib/sinatra/base.rb
753 def wlang(template, options = {}, locals = {}, &block)
754   render(:wlang, template, options, locals, &block)
755 end
yajl(template, options = {}, locals = {}) click to toggle source
    # File lib/sinatra/base.rb
757 def yajl(template, options = {}, locals = {})
758   options[:default_content_type] = :json
759   render :yajl, template, options, locals
760 end

Private Instance Methods

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

logic shared between builder and nokogiri

    # File lib/sinatra/base.rb
780 def render_ruby(engine, template, options = {}, locals = {}, &block)
781   options, template = template, nil if template.is_a?(Hash)
782   template = Proc.new { block } if template.nil?
783   render engine, template, options, locals
784 end