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