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