The Pug Automatic

Merb flat apps: layout and template paths

Written November 26, 2008. Tagged Ruby, Merb.

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:

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

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

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

and so on.