Merb flat apps: layout and template paths

Written . Tagged Merb, Ruby.

Layout and template paths in flat Merb apps (merb-gen flat myapp) are different from in regular apps.

Regular apps put layouts in app/views/layout, e.g. app/views/layout/application.html.erb; flat apps by default expect them in views, with a layout. prefix, e.g. views/layout.application.html.erb.

Also, flat apps by default don’t use per-controller view subdirectories, but only rely on the action name: views/index.html.erb for an “index” action and so on.

You can customize the paths in the _template_location method that the Merb generator will dump in your controller:

1
2
3
def _template_location(action, type = nil, controller = controller_name)
  controller == "layout" ? "layout.#{action}.#{type}" : "#{action}.#{type}"
end

It’s fairly straightforward as long as you know that when Merb looks for the layout, it will consider controller to be “layout” and action to be the layout name.

The type is the format – often “html”.

If you want all layouts in their own subdirectory, do something like

1
2
3
def _template_location(action, type = nil, controller = controller_name)
  controller == "layout" ? "layout/#{action}.#{type}" : "#{action}.#{type}"
end

If you want to include the controller in all template names, try

1
2
3
def _template_location(action, type = nil, controller = controller_name)
  "#{controller}.#{action}.#{type}"
end

and so on.