Module: Roda::RodaPlugins::RenderLocals

Defined in:
lib/roda/plugins/render_locals.rb

Overview

The render_locals plugin allows setting default locals for rendering templates.

plugin :render_locals, render: {heading: 'Hello'}

route do |r|
  r.get "foo" do
    view 'foo', locals: {name: 'Foo'} # locals: {:heading=>'Hello', :name=>'Foo'}
  end

  r.get "bar" do
    view 'foo', locals: {heading: 'Bar'} # locals: {:heading=>'Bar'}
  end

  view "default" # locals: {:heading=>'Hello'}
end

The render_locals plugin accepts the following options:

render

The default locals to use for template rendering

layout

The default locals to use for layout rendering

merge

Whether to merge template locals into layout locals

Defined Under Namespace

Modules: InstanceMethods

Class Method Summary collapse

Class Method Details

.configure(app, opts = OPTS) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/roda/plugins/render_locals.rb', line 34

def self.configure(app, opts=OPTS)
  app.opts[:render_locals] = (app.opts[:render_locals] || {}).merge(opts[:render]||{}).freeze
  app.opts[:layout_locals] = (app.opts[:layout_locals] || {}).merge(opts[:layout]||{}).freeze
  if opts.has_key?(:merge)
    app.opts[:merge_locals] = opts[:merge]
    app.opts[:layout_locals] = app.opts[:render_locals].merge(app.opts[:layout_locals]).freeze
  end
end

.load_dependencies(app, opts = OPTS) ⇒ Object



30
31
32
# File 'lib/roda/plugins/render_locals.rb', line 30

def self.load_dependencies(app, opts=OPTS)
  app.plugin :render
end