Module: Roda::RodaPlugins::ViewOptions

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

Overview

The view_options plugin allows you to override view and layout options and locals for specific branches and routes.

plugin :render
plugin :view_options

route do |r|
  r.on "users" do
    set_layout_options :template=>'users_layout'
    set_layout_locals :title=>'Users'
    set_view_options :engine=>'haml'
    set_view_locals :footer=>'(c) Roda'

    # ...
  end
end

The options and locals you specify have higher precedence than the render plugin options, but lower precedence than options you directly pass to the view/render methods.

View Subdirectories

The view_options plugin also has special support for sites that have outgrown a flat view directory and use subdirectories for views. It allows you to set the view directory to use, and template names that do not contain a slash will automatically use that view subdirectory. Example:

plugin :render, :layout=>'./layout'
plugin :view_options

route do |r|
  r.on "users" do
    set_view_subdir 'users'

    r.get :id do
      append_view_subdir 'profile'
      view 'index' # uses ./views/users/profile/index.erb
    end

    r.get 'list' do
      view 'lists/users' # uses ./views/lists/users.erb
    end
  end
end

Note that when a view subdirectory is set, the layout will also be looked up in the subdirectory unless it contains a slash. So if you want to use a view subdirectory for templates but have a shared layout, you should make sure your layout contains a slash, similar to the example above.

Per-branch HTML escaping

If you have an existing Roda application that doesn’t use automatic HTML escaping for <%= %> tags via the :render plugin’s :escape option, but you want to switch to using the :escape option, you can now do so without making all changes at once. With set_view_options, you can now specify escaping or not on a per branch basis in the routing tree:

plugin :render, :escape=>true
plugin :view_options

route do |r|
  # Don't escape <%= %> by default
  set_view_options :template_opts=>{:engine_class=>nil}

  r.on "users" do
    # Escape <%= %> in this branch
    set_view_options :template_opts=>{:engine_class=>render_opts[:template_opts][:engine_class]}
  end
end

Defined Under Namespace

Modules: InstanceMethods

Class Method Summary collapse

Class Method Details

.load_dependencies(app) ⇒ Object

Load the render plugin before this plugin, since this plugin works by overriding methods in the render plugin.



84
85
86
# File 'lib/roda/plugins/view_options.rb', line 84

def self.load_dependencies(app)
  app.plugin :render
end