class ContentController < ApplicationController
layout ‘example_layout’
end
then app/views/layout/example_layout.rhtml is used as the layout.
calling ‘render’ in the file.rhtml has no effect.
I’ve had a good trawl of docs and src - without finding anything
obvious. So I’m just wondering if someone has come across a solution or
if someone with better knowledge of rails source knows of an approach.
It may be possible to make it work the way you want, but the easiest way
to
make this work is to use an action in the controller to decide. The
whole
point of a controller is to decide what to do, and part of that can be
to
decide which layout to use.
Use the controller to control and the view to view.
The ContentController I’m using deals with arbitrary content in the app
i’m working on - pages with static content (apart from some dynamic bits
handled in layouts)
The reason I want to be able to control the layout in the rhtml file is
so I can keep the advantage of the default behaviour - (ie not define
actions - for every page I add)
I’d prefer something like <% layout ‘layout1’ %> in each of these
content rhtml files - than have to define an action like
def xxxx_page
render :action => ‘xxx_page’, :layout => ‘layout1’
end
for every new content file I add.
you may be thinking why do i need to change layouts like this within a
single controller - the answer is the web design being imposed on the
app i’m working on makes this necessary…
Just curious, what defines which rhtml file get’s which layout? If it’s
some attribute (database, param, session, etc …) then you can write
code
in the application.rb controller to switch layouts based on this
attribute.
I’ve done this, so it’s easy.
I don’t think it’s possible to have a view change its layout, as the
layout
has already been picked before the view gets rendered.
The documentation indicates you can do this be having a single layout
that
picks sub-layout file, calls
<%= render "sub-layout-file-name" %>
Your sub-layout code would do exactly as a layout does, including having
the @content_for_layout variable which contains the output from the action
rendering.
I define action ‘info’ in ContentController something like
def info
template_name = ‘content/info/#{}’ + params[:template]
render :template => template_name, :layout => ‘info_layout’
end
This is ok for me - I traced through how action controller handles
requests which don’t have actions - and couldn’t see any easy way to get
behaviour i originally wanted.
The documentation indicates you can do this be having a single layout
that picks sub-layout file,
Sorry - didn’t pick up on this the first time -
I could have ContentController - and all controllers choose the same
layout - but each of them set some variable and in the main layout
switch amongst sublayouts based on the value of the variable.
Which isn’t too bad a solution either - I think i’ll stick with the
routes.rb based solution for the moment.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.