module Roda::RodaPlugins::ViewOptions

  1. lib/roda/plugins/view_options.rb

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

plugin :render
plugin :view_options

route do |r|
  r.on "users" do
    set_layout_options template: 'users_layout'
    set_view_options engine: 'haml'

    # ...
  end
end

The options you specify via the set_view_options and set_layout_options methods 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 Integer do |id|
      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: {escape: false}

  r.on "users" do
    # Escape <%= %> in this branch
    set_view_options template_opts: {escape: true}
  end
end

Methods

Public Class

  1. load_dependencies

Public Class methods

load_dependencies(app)

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

[show source]
   # File lib/roda/plugins/view_options.rb
85 def self.load_dependencies(app)
86   app.plugin :render
87 end