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 704 def initialize 705 super 706 @default_layout = :layout 707 @preferred_extension = nil 708 end
Public Instance Methods
asciidoc(template, options = {}, locals = {})
click to toggle source
# File lib/sinatra/base.rb 766 def asciidoc(template, options = {}, locals = {}) 767 render :asciidoc, template, options, locals 768 end
builder(template = nil, options = {}, locals = {}, &block)
click to toggle source
# File lib/sinatra/base.rb 744 def builder(template = nil, options = {}, locals = {}, &block) 745 options[:default_content_type] = :xml 746 render_ruby(:builder, template, options, locals, &block) 747 end
coffee(template, options = {}, locals = {})
click to toggle source
# File lib/sinatra/base.rb 778 def coffee(template, options = {}, locals = {}) 779 options.merge! :layout => false, :default_content_type => :js 780 render :coffee, template, options, locals 781 end
creole(template, options = {}, locals = {})
click to toggle source
# File lib/sinatra/base.rb 792 def creole(template, options = {}, locals = {}) 793 render :creole, template, options, locals 794 end
erb(template, options = {}, locals = {}, &block)
click to toggle source
# File lib/sinatra/base.rb 710 def erb(template, options = {}, locals = {}, &block) 711 render(:erb, template, options, locals, &block) 712 end
erubis(template, options = {}, locals = {})
click to toggle source
# File lib/sinatra/base.rb 714 def erubis(template, options = {}, locals = {}) 715 warn "Sinatra::Templates#erubis is deprecated and will be removed, use #erb instead.\n" \ 716 "If you have Erubis installed, it will be used automatically." 717 render :erubis, template, options, locals 718 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 816 def find_template(views, name, engine) 817 yield ::File.join(views, "#{name}.#{@preferred_extension}") 818 819 Tilt.default_mapping.extensions_for(engine).each do |ext| 820 yield ::File.join(views, "#{name}.#{ext}") unless ext == @preferred_extension 821 end 822 end
haml(template, options = {}, locals = {}, &block)
click to toggle source
# File lib/sinatra/base.rb 720 def haml(template, options = {}, locals = {}, &block) 721 render(:haml, template, options, locals, &block) 722 end
less(template, options = {}, locals = {})
click to toggle source
# File lib/sinatra/base.rb 734 def less(template, options = {}, locals = {}) 735 options.merge! :layout => false, :default_content_type => :css 736 render :less, template, options, locals 737 end
liquid(template, options = {}, locals = {}, &block)
click to toggle source
# File lib/sinatra/base.rb 749 def liquid(template, options = {}, locals = {}, &block) 750 render(:liquid, template, options, locals, &block) 751 end
markaby(template = nil, options = {}, locals = {}, &block)
click to toggle source
# File lib/sinatra/base.rb 774 def markaby(template = nil, options = {}, locals = {}, &block) 775 render_ruby(:mab, template, options, locals, &block) 776 end
markdown(template, options = {}, locals = {})
click to toggle source
# File lib/sinatra/base.rb 753 def markdown(template, options = {}, locals = {}) 754 options[:exclude_outvar] = true 755 render :markdown, template, options, locals 756 end
mediawiki(template, options = {}, locals = {})
click to toggle source
# File lib/sinatra/base.rb 796 def mediawiki(template, options = {}, locals = {}) 797 render :mediawiki, template, options, locals 798 end
nokogiri(template = nil, options = {}, locals = {}, &block)
click to toggle source
# File lib/sinatra/base.rb 783 def nokogiri(template = nil, options = {}, locals = {}, &block) 784 options[:default_content_type] = :xml 785 render_ruby(:nokogiri, template, options, locals, &block) 786 end
rabl(template, options = {}, locals = {})
click to toggle source
# File lib/sinatra/base.rb 809 def rabl(template, options = {}, locals = {}) 810 Rabl.register! 811 render :rabl, template, options, locals 812 end
radius(template, options = {}, locals = {})
click to toggle source
# File lib/sinatra/base.rb 770 def radius(template, options = {}, locals = {}) 771 render :radius, template, options, locals 772 end
rdoc(template, options = {}, locals = {})
click to toggle source
# File lib/sinatra/base.rb 762 def rdoc(template, options = {}, locals = {}) 763 render :rdoc, template, options, locals 764 end
sass(template, options = {}, locals = {})
click to toggle source
# File lib/sinatra/base.rb 724 def sass(template, options = {}, locals = {}) 725 options.merge! :layout => false, :default_content_type => :css 726 render :sass, template, options, locals 727 end
scss(template, options = {}, locals = {})
click to toggle source
# File lib/sinatra/base.rb 729 def scss(template, options = {}, locals = {}) 730 options.merge! :layout => false, :default_content_type => :css 731 render :scss, template, options, locals 732 end
slim(template, options = {}, locals = {}, &block)
click to toggle source
# File lib/sinatra/base.rb 788 def slim(template, options = {}, locals = {}, &block) 789 render(:slim, template, options, locals, &block) 790 end
stylus(template, options = {}, locals = {})
click to toggle source
# File lib/sinatra/base.rb 739 def stylus(template, options = {}, locals = {}) 740 options.merge! :layout => false, :default_content_type => :css 741 render :styl, template, options, locals 742 end
textile(template, options = {}, locals = {})
click to toggle source
# File lib/sinatra/base.rb 758 def textile(template, options = {}, locals = {}) 759 render :textile, template, options, locals 760 end
wlang(template, options = {}, locals = {}, &block)
click to toggle source
# File lib/sinatra/base.rb 800 def wlang(template, options = {}, locals = {}, &block) 801 render(:wlang, template, options, locals, &block) 802 end
yajl(template, options = {}, locals = {})
click to toggle source
# File lib/sinatra/base.rb 804 def yajl(template, options = {}, locals = {}) 805 options[:default_content_type] = :json 806 render :yajl, template, options, locals 807 end
Private Instance Methods
compile_block_template(template, options, &body)
click to toggle source
# File lib/sinatra/base.rb 916 def compile_block_template(template, options, &body) 917 caller = settings.caller_locations.first 918 path = options[:path] || caller[0] 919 line = options[:line] || caller[1] 920 template.new(path, line.to_i, options, &body) 921 end
compile_template(engine, data, options, views)
click to toggle source
# File lib/sinatra/base.rb 879 def compile_template(engine, data, options, views) 880 eat_errors = options.delete :eat_errors 881 template = Tilt[engine] 882 raise "Template engine not found: #{engine}" if template.nil? 883 884 case data 885 when Symbol 886 template_cache.fetch engine, data, options, views do 887 body, path, line = settings.templates[data] 888 if body 889 body = body.call if body.respond_to?(:call) 890 template.new(path, line.to_i, options) { body } 891 else 892 found = false 893 @preferred_extension = engine.to_s 894 find_template(views, data, template) do |file| 895 path ||= file # keep the initial path rather than the last one 896 if found = File.exist?(file) 897 path = file 898 break 899 end 900 end 901 throw :layout_missing if eat_errors and not found 902 template.new(path, 1, options) 903 end 904 end 905 when Proc 906 compile_block_template(template, options, &data) 907 when String 908 template_cache.fetch engine, data, options, views do 909 compile_block_template(template, options) { data } 910 end 911 else 912 raise ArgumentError, "Sorry, don't know how to render #{data.inspect}." 913 end 914 end
render(engine, data, options = {}, locals = {}, &block)
click to toggle source
# File lib/sinatra/base.rb 833 def render(engine, data, options = {}, locals = {}, &block) 834 # merge app-level options 835 engine_options = settings.respond_to?(engine) ? settings.send(engine) : {} 836 options.merge!(engine_options) { |key, v1, v2| v1 } 837 838 # extract generic options 839 locals = options.delete(:locals) || locals || {} 840 views = options.delete(:views) || settings.views || "./views" 841 layout = options[:layout] 842 layout = false if layout.nil? && options.include?(:layout) 843 eat_errors = layout.nil? 844 layout = engine_options[:layout] if layout.nil? or (layout == true && engine_options[:layout] != false) 845 layout = @default_layout if layout.nil? or layout == true 846 layout_options = options.delete(:layout_options) || {} 847 content_type = options.delete(:default_content_type) 848 content_type = options.delete(:content_type) || content_type 849 layout_engine = options.delete(:layout_engine) || engine 850 scope = options.delete(:scope) || self 851 exclude_outvar = options.delete(:exclude_outvar) 852 options.delete(:layout) 853 854 # set some defaults 855 options[:outvar] ||= '@_out_buf' unless exclude_outvar 856 options[:default_encoding] ||= settings.default_encoding 857 858 # compile and render template 859 begin 860 layout_was = @default_layout 861 @default_layout = false 862 template = compile_template(engine, data, options, views) 863 output = template.render(scope, locals, &block) 864 ensure 865 @default_layout = layout_was 866 end 867 868 # render layout 869 if layout 870 options = options.merge(:views => views, :layout => false, :eat_errors => eat_errors, :scope => scope). 871 merge!(layout_options) 872 catch(:layout_missing) { return render(layout_engine, layout, options, locals) { output } } 873 end 874 875 output.extend(ContentTyped).content_type = content_type if content_type 876 output 877 end
render_ruby(engine, template, options = {}, locals = {}, &block)
click to toggle source
logic shared between builder and nokogiri
# File lib/sinatra/base.rb 827 def render_ruby(engine, template, options = {}, locals = {}, &block) 828 options, template = template, nil if template.is_a?(Hash) 829 template = Proc.new { block } if template.nil? 830 render engine, template, options, locals 831 end