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