The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Don't Use Sub Templates

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Obie Fernandez

Posts: 608
Nickname: obie
Registered: Aug, 2005

Obie Fernandez is a Technologist for ThoughtWorks
Don't Use Sub Templates Posted: May 24, 2007 11:48 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Obie Fernandez.
Original Post: Don't Use Sub Templates
Feed Title: Obie On Rails (Has It Been 9 Years Already?)
Feed URL: http://jroller.com/obie/feed/entries/rss
Feed Description: Obie Fernandez talks about life as a technologist, mostly as ramblings about software development and consulting. Nowadays it's pretty much all about Ruby and Ruby on Rails.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Obie Fernandez
Latest Posts From Obie On Rails (Has It Been 9 Years Already?)

Advertisement

Sub templates is an old feature of Rails that hasn't gotten much attention lately and should not be used in modern Rails applications. I looked into it this evening while working on my view chapter for The Rails Way, and got the answer straight from David and Koz. I don't know if the feature will be deprecated anytime soon, but rest assured you should be using render :partial => ... instead.

Don't know what I'm talking about?

Using sub templates allows you to sidestep tedious replication and extract common display structures in shared templates. The classic example is the use of a header and footer (even though the Action Pack-way would be to use Layouts):

  <%= render "shared/header" %>
  Something really specific and terrific
  <%= render "shared/footer" %>

As you see, we use the output embeddings for the render methods. The render call itself will just return a string holding the result of the rendering. The output embedding writes it to the current template.

But you don?t have to restrict yourself to static includes. Templates can share variables amongst themselves by using instance variables defined using the regular embedding tags. Like this:

  <% @page_title = "A Wonderful Hello" %>
  <%= render "shared/header" %>

Now the header can pick up on the @page_title variable and use it for outputting a title tag:

  <title><%= @page_title %></title>

Read: Don&#39;t Use Sub Templates

Topic: stylesheet_include Previous Topic   Next Topic Topic: RSpec 1.0.0 - very nice syntax highlighting

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use