Module: Sinatra::MultiRender::Helpers

Defined in:
lib/sinatra/support/multirender.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) show(template, options = {}, locals = {}, &block)

Shows a given template.

You may pass options to specify which engines you will want to limit the search to. This defaults to App.multi_engines, or Tilt's supported engines if that's not defined.

You may pass options to specify which directories to scour. This defaults to App.multi_views, or App.views.

Examples

# Only HAML
show :home, engine: :haml

# HAML or ERB, favoring HAML first
show :home, engine: [:haml, :erb]

# Only look at certain paths
show :home, views: [ './views', './skins/default' ]


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sinatra/support/multirender.rb', line 58

def show(template, options={}, locals={}, &block)
  paths     = [*(options[:views]  || settings.multi_views)].join(',')
  engines   = [*(options[:engine] || settings.multi_engines)].join(',')

  t = @template_cache.fetch template, options do
    spec = "{#{paths}}/#{template}.{#{engines}}"
    template = Dir[spec].first  or raise Errno::ENOENT.new(spec)

    ext      = File.extname(template)[1..-1].to_sym
    options  = settings.send(ext).merge(options)  if settings.respond_to?(ext)
    engine   = Tilt[ext]  or raise "Template engine not found: #{ext}"

    engine.new(template, 0, options)
  end

  t.render(self, locals, &block)  if t
end