SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict DevilBear's Avatar
    Join Date
    Oct 2001
    Location
    Hades
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

    Like this:
    Code:
    <% menu = ['list', 'edit', 'create'] %>
    
    <% for action in menu %>
      <% if action == params[:action] %>
      <strong><%= action %></strong %>
      <% else %>
      <%= action %>
      <% end %>
      <br />
    <% end %>
    Or if you need more flexibility:

    Code:
    <%
    menu = {
      'List of items' => lambda{params[:action] == 'list'},
      'Create a new item' => lambda{params[:action] == 'new'},
      'Edit an item'=> lambda{params[:action] == 'edit'}
      }
     %>
    
    <% for (text, pred) in menu %>
      <% if pred.call %>
      <strong><%= text %></strong %>
      <% else %>
      <%= text %>
      <% end %>
      <br />
    <% end %>

  3. #3
    SitePoint Addict DevilBear's Avatar
    Join Date
    Oct 2001
    Location
    Hades
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that's a good start for me...

    I don't think I can use the action to differentiate, because the navigation buttons link to the index actions of different controllers... any idea how to handle that?

    -Josh

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The controller is stored in params[:controller], so you can do this:
    Code:
    <% menu = ['list', 'edit', 'create'] %>
    
    <% for action in menu %>
      <% if action == params[:controller] %>
      <strong><%= action %></strong %>
      <% else %>
      <%= action %>
      <% end %>
      <br />
    <% end %>

  5. #5
    SitePoint Addict DevilBear's Avatar
    Join Date
    Oct 2001
    Location
    Hades
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does the partial know what layout it's being rendered by?

  6. #6
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know, but I think it does know which controller, because that's in the params hash. Does this code work?

  7. #7
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by DevilBear
    Does the partial know what layout it's being rendered by?
    It looks like ActionController::Base doesn't expose which layout it's using programmatically, but if you know which controller uses which layout yourself you can work something out.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •