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