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