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