[LRUG] Rails : Drying Up Views

Anthony Green Anthony.Green at bbc.co.uk
Wed Sep 12 01:51:34 PDT 2007




On 11/9/07 22:10, "Tom Armitage" <tom at infovore.org> wrote:

> Various options for your "sidebar" example, notably, providing
> everything you want the sidebar in a different layout.

Resulted in too much HTML code duplication

> Or conditional  
> stuff (ew) in the layout.

Which is where I'm at now and yes very ew

>Or, as I've done in places, a directory
> called /views/partial and then calling render on "/views/
> partial" (opening slash important).

I guess its conditionally included partials that are giving me grief.

There appears to be no clean mechanism for dealing with these. The best I
can come up with so for is to yield to a blocks within a shared partial that
sets up those other blocks of HTML code.

ie 

/views/layouts /application.rhtml
-----------------------------------
<HTML>
<BODY>

<%= yield :some_html_only_in_action_name_view %>

<h1>Some text in all views</h1>

<%= render :partial => 'some_partial' %>

<%= yield :layout -%>

<%= render :partial => 'some_other_partial' %>

</BODY>
</HTML>
-----------------------------------


/views/shared/action_name.rhtml
-----------------------------------
<% content_for :some_html_only_in_action_name_view do %>

  <b>Some text</b>
  <%= helper_method_or_other_bit_of_code_whatever -%>
  <em>some more text</b>

<% end %>

<h2>This is the action_)name page</h2>

<%= render :partial => 'shared_partial' %>

-----------------------------------


There's a nice article on drying up views here:

http://codefluency.com/2007/5/18/when-v-is-for-vexing




-- 
Anthony Green
Client Side Developer - Programme Information Tool
Audio & Music Interactive
BBC Future Media & Technology












More information about the Chat mailing list