The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

How to break up my HTML for Rails

So I'm transitioning away from PHP/all-my-code-and-html-together to Rails. Half the time that's "woot" and the other half it's "whew" <laugh> A few questions now... :-)

I'm used to putting my website mast/navigation into, say, mast.php and then include()ing it from index.php, section1.php, etc.

Each of the main pages, what in Rails will be controllers, would set a variable like $page = 'index', $page = 'section1'. mast.php had automatic access to that variable and could do things with it... like, "If we're being called from section1, don't put a link around the section1 button... instead, show the section1_rollover graphic."

Now in Rails... I want to separate out the mast code (and keep DRY), but does it go in a normal controller/template pair, should it be a component, etc? I wouldn't want it to be able to be called from myapp.com/mast for example.

This may influence the answer: the mast html needs to get called from a layout file, which will also have other content_for_layout to render.

So the pieces are something like:

somecontroller wants to render someview, with somelayout wrapped around it...

somelayout wants to include the mastview

Finally, I anticipate the particular project I'm working on having about three layouts... each one needs to tell the mast controller if they have a sidebar or not, so that the mast view can render some bits of HTML one way versus another.

I have no idea how to pass that on to the mast controller from the layout :-)

Sorry if this is unclear, and I'm trying to ask several different things in one post. Just trying to wrap my head around how to do all of this, and I'm already so used to doing it the PHP way.

I think that a component would be an overkill...but there are two other solutions:

1. A helper method.
2. A partial.

A helper method is a method in app/helpers/application_helper.rb. This method will be available in the layout/view. So if you define a create_navigation() helper method, you can do this in your layout:

Code:

<%= create_navigation() %>

A partial works more like PHP's include. Put a file called _navigation.rhtml in app/views/layouts/, and write the code to create the navigation. Now you can do this in your layout file:

Code:

<%= render :partial => 'layouts/navigation' %>

This will do the same as:

Code:

<?php include 'layouts/_navigation.php' ?>

You don't have to tell the navigation code which button should be highlighted, because you can get that information from the action.