3 # The magic or rendering happens here. This is included in Sinatra::EventContext on load.
5 # These methods are the foundation for Sinatra::Erb and Sinatra::Haml and allow you to quickly
6 # create custom wrappers for your favorite rendering engines outside of erb and haml.
10 Layouts = Hash.new # :nodoc:
13 :views_directory => 'views',
18 # Renders templates from a string or file and handles their layouts:
22 # def my(template, options, &layout)
23 # render(template, :my, options, &layout)
26 # def render_my(template)
27 # template.capitalize # It capitalizes templates!!!!! WOW!
30 # Sinatra::EventContext.send :include, MyRenderer
36 # get_it '/' # => 'Something'
38 # The second method is named render_extname. render will call this dynamicly
41 # * +template+ If String, renders the string. If Symbol, reads from file with the basename of the Symbol; uses +renderer+ for extension.
42 # * +renderer+ A symbol defining the render_ method to call and the extension append to +template+ when looking in the +views_directory+
43 # * +options+ An optional Hash of options (see next section)
46 # * +:views_directory+ Allows you to override the default 'views' directory an look for the template in another
47 # * +:layout+ Which layout to use (see Sinatra::Dsl). false to force a render with no layout. Defaults to :default layout
49 def render(template, renderer, options = {})
50 options = DEFAULT_OPTIONS.merge(options)
52 layout = block_given? ? yield : Layouts[options[:layout]]
54 result_method = 'render_%s' % renderer
57 send(result_method, layout) { send(result_method, determine_template(template, renderer, options)) }
59 send(result_method, determine_template(template, renderer, options))
65 def determine_template(template, ext, options)
66 if template.is_a?(Symbol)
67 File.read("%s/%s.%s" % [options[:views_directory], template, ext])