module Roda::RodaPlugins::Render::InstanceMethods

Public Instance Methods

render(template, opts = {}, &block) click to toggle source

Render the given template. See Render for details.

# File lib/roda/plugins/render.rb, line 123
def render(template, opts = {}, &block)
  if template.is_a?(Hash)
    if opts.empty?
      opts = template
    else
      opts = opts.merge(template)
    end
  end
  render_opts = render_opts()

  if content = opts[:inline]
    path = content
    template_block = Proc.new{content}
    template_class = ::Tilt[opts[:engine] || render_opts[:engine]]
  else
    template_class = ::Tilt
    unless path = opts[:path]
      path = template_path(template, opts)
    end
  end

  cached_template(path) do
    template_class.new(path, 1, render_opts[:opts].merge(opts), &template_block)
  end.render(self, opts[:locals], &block)
end
render_opts() click to toggle source

Return the render options for the instance’s class.

# File lib/roda/plugins/render.rb, line 150
def render_opts
  self.class.render_opts
end
view(template, opts={}) click to toggle source

Render the given template. If there is a default layout for the class, take the result of the template rendering and render it inside the layout. See Render for details.

# File lib/roda/plugins/render.rb, line 157
def view(template, opts={})
  if template.is_a?(Hash)
    if opts.empty?
      opts = template
    else
      opts = opts.merge(template)
    end
  end

  content = opts[:content] || render(template, opts)

  if layout = opts.fetch(:layout, render_opts[:layout])
    if layout_opts = opts[:layout_opts]
      layout_opts = render_opts[:layout_opts].merge(layout_opts)
    end

    content = render(layout, layout_opts||{}){content}
  end

  content
end

Private Instance Methods

cached_template(path) { || ... } click to toggle source

If caching templates, attempt to retrieve the template from the cache. Otherwise, just yield to get the template.

# File lib/roda/plugins/render.rb, line 183
def cached_template(path, &block)
  if cache = render_opts[:cache]
    unless template = cache[path]
      template = cache[path] = yield
    end
    template
  else
    yield
  end
end
template_path(template, opts) click to toggle source

The path for the given template.

# File lib/roda/plugins/render.rb, line 195
def template_path(template, opts)
  render_opts = render_opts()
  "#{opts[:views] || render_opts[:views]}/#{template}.#{opts[:ext] || render_opts[:ext] || render_opts[:engine]}"
end