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`) :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
Source
# File lib/sinatra/base.rb 749 def initialize 750 super 751 @default_layout = :layout 752 @preferred_extension = nil 753 end
Calls superclass method
Public Instance Methods
Source
# File lib/sinatra/base.rb 795 def asciidoc(template, options = {}, locals = {}) 796 render :asciidoc, template, options, locals 797 end
Source
# File lib/sinatra/base.rb 777 def builder(template = nil, options = {}, locals = {}, &block) 778 options[:default_content_type] = :xml 779 render_ruby(:builder, template, options, locals, &block) 780 end
Source
# File lib/sinatra/base.rb 755 def erb(template, options = {}, locals = {}, &block) 756 render(:erb, template, options, locals, &block) 757 end
Source
# File lib/sinatra/base.rb 824 def find_template(views, name, engine) 825 yield ::File.join(views, "#{name}.#{@preferred_extension}") 826 827 Tilt.default_mapping.extensions_for(engine).each do |ext| 828 yield ::File.join(views, "#{name}.#{ext}") unless ext == @preferred_extension 829 end 830 end
Calls the given block for every possible template file in views, named name.ext, where ext is registered on engine.
Source
# File lib/sinatra/base.rb 759 def haml(template, options = {}, locals = {}, &block) 760 render(:haml, template, options, locals, &block) 761 end
Source
# File lib/sinatra/base.rb 782 def liquid(template, options = {}, locals = {}, &block) 783 render(:liquid, template, options, locals, &block) 784 end
Source
# File lib/sinatra/base.rb 799 def markaby(template = nil, options = {}, locals = {}, &block) 800 render_ruby(:mab, template, options, locals, &block) 801 end
Source
# File lib/sinatra/base.rb 786 def markdown(template, options = {}, locals = {}) 787 options[:exclude_outvar] = true 788 render :markdown, template, options, locals 789 end
Source
# File lib/sinatra/base.rb 803 def nokogiri(template = nil, options = {}, locals = {}, &block) 804 options[:default_content_type] = :xml 805 render_ruby(:nokogiri, template, options, locals, &block) 806 end
Source
# File lib/sinatra/base.rb 817 def rabl(template, options = {}, locals = {}) 818 Rabl.register! 819 render :rabl, template, options, locals 820 end
Source
# File lib/sinatra/base.rb 791 def rdoc(template, options = {}, locals = {}) 792 render :rdoc, template, options, locals 793 end
Source
# File lib/sinatra/base.rb 763 def sass(template, options = {}, locals = {}) 764 options[:default_content_type] = :css 765 options[:exclude_outvar] = true 766 options[:layout] = nil 767 render :sass, template, options, locals 768 end
Source
# File lib/sinatra/base.rb 770 def scss(template, options = {}, locals = {}) 771 options[:default_content_type] = :css 772 options[:exclude_outvar] = true 773 options[:layout] = nil 774 render :scss, template, options, locals 775 end
Source
# File lib/sinatra/base.rb 808 def slim(template, options = {}, locals = {}, &block) 809 render(:slim, template, options, locals, &block) 810 end
Source
# File lib/sinatra/base.rb 812 def yajl(template, options = {}, locals = {}) 813 options[:default_content_type] = :json 814 render :yajl, template, options, locals 815 end
Private Instance Methods
Source
# File lib/sinatra/base.rb 933 def compile_block_template(template, options, &body) 934 first_location = caller_locations.first 935 path = first_location.path 936 line = first_location.lineno 937 path = options[:path] || path 938 line = options[:line] || line 939 template.new(path, line.to_i, options, &body) 940 end
Source
# File lib/sinatra/base.rb 895 def compile_template(engine, data, options, views) 896 eat_errors = options.delete :eat_errors 897 template = Tilt[engine] 898 raise "Template engine not found: #{engine}" if template.nil? 899 900 case data 901 when Symbol 902 template_cache.fetch engine, data, options, views do 903 body, path, line = settings.templates[data] 904 if body 905 body = body.call if body.respond_to?(:call) 906 template.new(path, line.to_i, options) { body } 907 else 908 found = false 909 @preferred_extension = engine.to_s 910 find_template(views, data, template) do |file| 911 path ||= file # keep the initial path rather than the last one 912 found = File.exist?(file) 913 if found 914 path = file 915 break 916 end 917 end 918 throw :layout_missing if eat_errors && !found 919 template.new(path, 1, options) 920 end 921 end 922 when Proc 923 compile_block_template(template, options, &data) 924 when String 925 template_cache.fetch engine, data, options, views do 926 compile_block_template(template, options) { data } 927 end 928 else 929 raise ArgumentError, "Sorry, don't know how to render #{data.inspect}." 930 end 931 end
Source
# File lib/sinatra/base.rb 844 def render(engine, data, options = {}, locals = {}, &block) 845 # merge app-level options 846 engine_options = settings.respond_to?(engine) ? settings.send(engine) : {} 847 options.merge!(engine_options) { |_key, v1, _v2| v1 } 848 849 # extract generic options 850 locals = options.delete(:locals) || locals || {} 851 views = options.delete(:views) || settings.views || './views' 852 layout = options[:layout] 853 layout = false if layout.nil? && options.include?(:layout) 854 eat_errors = layout.nil? 855 layout = engine_options[:layout] if layout.nil? || (layout == true && engine_options[:layout] != false) 856 layout = @default_layout if layout.nil? || (layout == true) 857 layout_options = options.delete(:layout_options) || {} 858 content_type = options.delete(:default_content_type) 859 content_type = options.delete(:content_type) || content_type 860 layout_engine = options.delete(:layout_engine) || engine 861 scope = options.delete(:scope) || self 862 exclude_outvar = options.delete(:exclude_outvar) 863 options.delete(:layout) 864 865 # set some defaults 866 options[:outvar] ||= '@_out_buf' unless exclude_outvar 867 options[:default_encoding] ||= settings.default_encoding 868 869 # compile and render template 870 begin 871 layout_was = @default_layout 872 @default_layout = false 873 template = compile_template(engine, data, options, views) 874 output = template.render(scope, locals, &block) 875 ensure 876 @default_layout = layout_was 877 end 878 879 # render layout 880 if layout 881 extra_options = { views: views, layout: false, eat_errors: eat_errors, scope: scope } 882 options = options.merge(extra_options).merge!(layout_options) 883 884 catch(:layout_missing) { return render(layout_engine, layout, options, locals) { output } } 885 end 886 887 if content_type 888 # sass-embedded returns a frozen string 889 output = +output 890 output.extend(ContentTyped).content_type = content_type 891 end 892 output 893 end
Source
# File lib/sinatra/base.rb 835 def render_ruby(engine, template, options = {}, locals = {}, &block) 836 if template.is_a?(Hash) 837 options = template 838 template = nil 839 end 840 template = proc { block } if template.nil? 841 render engine, template, options, locals 842 end
logic shared between builder and nokogiri