[LRUG] Rails : Drying Up Views
Tom Armitage
tom at infovore.org
Wed Sep 12 02:09:13 PDT 2007
Ah - so what you're looking for is a conditional content_for statement,
right? This is the solution we used at work (which Jared, a colleague
from Thoughtbot, came up with):
http://giantrobots.thoughtbot.com/2007/7/2/no-content-for-you
content_for is a great tool, if people aren't aware of it. It harnesses
the power of how rails views are generated (from the inside out, ie: you
can refer to stuff in the layout that you generate in the view) to let
you put lots of things like this into the view code. Again, something we
made a lot of use at on our current project at work.
Does that help at all, Anthony?
On 9/12/2007, "Anthony Green" <Anthony.Green at bbc.co.uk> wrote:
>
>
>
>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
>
>
>
>
>
>
>
>
>
>_______________________________________________
>Chat mailing list
>Chat at lists.lrug.org
>http://lists.lrug.org/listinfo.cgi/chat-lrug.org
More information about the Chat
mailing list