Forum: Ruby on Rails Advice on Similar Views with Helpers

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
29d82d169c6037d4cbafce0b20fcf04e?d=identicon&s=25 The Barge (thebarge)
on 2006-04-10 18:34
I have some embedded tables that form different sections of a view.  I
find that I use the same look for certain ones which means I have to
duplicate the same exact table for each section that's similar.

Is there a way to do.. hmm.. well.. kind of like what a layout does
using @content_for_layout, but for a partial.  Does that make any sense
whatsoever? :)

OK so here's some pseudocode of maybe a helper or something..

<% view_admin_block "Admin Title" %>
    This is some content that would go in that block like links to
changing usernames, etc.
<% end %>

What this would do is render a table with all the fancy look-n-feel,
borders, all the things that it needs, and it would place "User
Administration" in the cell it should go in, and all the content in
another one.  So something like...

<table border="0" cellpadding="0" cellspacing="0">
<tr><td class="admin_title"><%= the_title %></td></tr>
<tr><td class="admin_content"><%= @content_for_partial %></td></tr>
</table>

This is a very simplistic example, but my tables are fairly complex with
spacers, different classes, styles, etc.

I bring this up because I just noticed that one of my table designs
didnt render correctly in IE (big shocker right?).  So I have to go
through every single partial that uses that same look and fix it to work
with IE.
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-04-10 18:39
The Barge wrote:
> I have some embedded tables that form different sections of a view.  I
> find that I use the same look for certain ones which means I have to
> duplicate the same exact table for each section that's similar.
>
> Is there a way to do.. hmm.. well.. kind of like what a layout does
> using @content_for_layout, but for a partial.  Does that make any sense
> whatsoever? :)
>
> OK so here's some pseudocode of maybe a helper or something..
>
> <% view_admin_block "Admin Title" %>
>     This is some content that would go in that block like links to
> changing usernames, etc.
> <% end %>
>
> What this would do is render a table with all the fancy look-n-feel,
> borders, all the things that it needs, and it would place "User
> Administration" in the cell it should go in, and all the content in
> another one.  So something like...
>
> <table border="0" cellpadding="0" cellspacing="0">
> <tr><td class="admin_title"><%= the_title %></td></tr>
> <tr><td class="admin_content"><%= @content_for_partial %></td></tr>
> </table>
>
> This is a very simplistic example, but my tables are fairly complex with
> spacers, different classes, styles, etc.
>
> I bring this up because I just noticed that one of my table designs
> didnt render correctly in IE (big shocker right?).  So I have to go
> through every single partial that uses that same look and fix it to work
> with IE.

You can send variables to a partial, which can then become the content.

render :partial => 'table', :locals => {:the_title => @title, :content
=> @my_content}

and then

<table border="0" cellpadding="0" cellspacing="0">
<tr><td class="admin_title"><%= the_title %></td></tr>
<tr><td class="admin_content"><%= content %></td></tr>
</table>

Jeff
29d82d169c6037d4cbafce0b20fcf04e?d=identicon&s=25 The Barge (thebarge)
on 2006-04-10 20:48
Jeff Coleman wrote:
> You can send variables to a partial, which can then become the content.
>
> render :partial => 'table', :locals => {:the_title => @title, :content
> => @my_content}
>
> and then
>
> <table border="0" cellpadding="0" cellspacing="0">
> <tr><td class="admin_title"><%= the_title %></td></tr>
> <tr><td class="admin_content"><%= content %></td></tr>
> </table>

Yeah, but the content could be a lot of RHTML code depending on how much
is displayed in the table block.  I'll check into this more though.  It
at least gets me heading in the right direction.
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-04-10 22:06
The Barge wrote:
> Jeff Coleman wrote:
>> You can send variables to a partial, which can then become the content.
>>
>> render :partial => 'table', :locals => {:the_title => @title, :content
>> => @my_content}
>>
>> and then
>>
>> <table border="0" cellpadding="0" cellspacing="0">
>> <tr><td class="admin_title"><%= the_title %></td></tr>
>> <tr><td class="admin_content"><%= content %></td></tr>
>> </table>
>
> Yeah, but the content could be a lot of RHTML code depending on how much
> is displayed in the table block.  I'll check into this more though.  It
> at least gets me heading in the right direction.

You can call a partial from within a partial.  Why not do that?

Jeff
29d82d169c6037d4cbafce0b20fcf04e?d=identicon&s=25 The Barge (thebarge)
on 2006-04-10 22:53
Jeff Coleman wrote:
> The Barge wrote:
>> Jeff Coleman wrote:
>>> You can send variables to a partial, which can then become the content.
>>>
>>> render :partial => 'table', :locals => {:the_title => @title, :content
>>> => @my_content}
>>>
>>> and then
>>>
>>> <table border="0" cellpadding="0" cellspacing="0">
>>> <tr><td class="admin_title"><%= the_title %></td></tr>
>>> <tr><td class="admin_content"><%= content %></td></tr>
>>> </table>
>>
>> Yeah, but the content could be a lot of RHTML code depending on how much
>> is displayed in the table block.  I'll check into this more though.  It
>> at least gets me heading in the right direction.
>
> You can call a partial from within a partial.  Why not do that?

Not sure how this would help me?  OK from the main view, lets say
"users.rhtml" I render a partial called "admin_accounts".  Then, what
would it do?  Would it render another more generalized partial that
represents the look-n-feel (i.e. the table above)?  Wouldn't I need at
least two partials for that?  One representing the top-half (before <%=
content %>) and a second for the bottom half so I could surround the
"content" by the rest of the document?

<%= render_partial "admin_accounts1" %>
Here is the content that goes in this place
<%= render_partial "admin_accounts2" %>

I've been staring at this too long I think :)  The solution is probably
staring me right in the face.
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-04-10 23:42
The Barge wrote:
> Jeff Coleman wrote:
>> The Barge wrote:
>>> Jeff Coleman wrote:
>>>> You can send variables to a partial, which can then become the content.
>>>>
>>>> render :partial => 'table', :locals => {:the_title => @title, :content
>>>> => @my_content}
>>>>
>>>> and then
>>>>
>>>> <table border="0" cellpadding="0" cellspacing="0">
>>>> <tr><td class="admin_title"><%= the_title %></td></tr>
>>>> <tr><td class="admin_content"><%= content %></td></tr>
>>>> </table>
>>>
>>> Yeah, but the content could be a lot of RHTML code depending on how much
>>> is displayed in the table block.  I'll check into this more though.  It
>>> at least gets me heading in the right direction.
>>
>> You can call a partial from within a partial.  Why not do that?
>
> Not sure how this would help me?  OK from the main view, lets say
> "users.rhtml" I render a partial called "admin_accounts".  Then, what
> would it do?  Would it render another more generalized partial that
> represents the look-n-feel (i.e. the table above)?  Wouldn't I need at
> least two partials for that?  One representing the top-half (before <%=
> content %>) and a second for the bottom half so I could surround the
> "content" by the rest of the document?
>
> <%= render_partial "admin_accounts1" %>
> Here is the content that goes in this place
> <%= render_partial "admin_accounts2" %>
>
> I've been staring at this too long I think :)  The solution is probably
> staring me right in the face.

There may be a better way to do it, but you could pass the name of the
partial to be rendered as a local variable to your "template" partial.

render :partial => 'table', :locals => {:content_for_partial =>
'admin_accounts'}

_table.rhtml:

<table border="0" cellpadding="0" cellspacing="0">
<tr><td class="admin_title"><%= the_title %></td></tr>
<tr><td class="admin_content"><%= render :partial => content_for_partial
%></td></tr>
</table>

Again, there may be a better way, but this is at least one option
available.

Jeff Coleman
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-04-10 23:44
Jeff Coleman wrote:

> render :partial => 'table', :locals => {:content_for_partial =>
> 'admin_accounts'}
>
> _table.rhtml:
>
> <table border="0" cellpadding="0" cellspacing="0">
> <tr><td class="admin_title"><%= the_title %></td></tr>
> <tr><td class="admin_content"><%= render :partial => content_for_partial
> %></td></tr>
> </table>

Copied-and-pasted my own code without changing it completely.  You'd
want to either send "the_title" as a local variable to the "table"
partial, or set @the_title in your controller and use that instead.

Jeff Coleman
29d82d169c6037d4cbafce0b20fcf04e?d=identicon&s=25 The Barge (thebarge)
on 2006-04-13 20:24
Jeff Coleman wrote:
> There may be a better way to do it, but you could pass the name of the
> partial to be rendered as a local variable to your "template" partial.
>
> render :partial => 'table', :locals => {:content_for_partial =>
> 'admin_accounts'}
>
> _table.rhtml:
>
> <table border="0" cellpadding="0" cellspacing="0">
> <tr><td class="admin_title"><%= the_title %></td></tr>
> <tr><td class="admin_content"><%= render :partial => content_for_partial
> %></td></tr>
> </table>
>
> Again, there may be a better way, but this is at least one option
> available.

Sorry for the late reply, but this is working PERFECTLY.  I knew there
had to be a way to do this so I could encapsulate different parts of the
page so everything wasn't crammed into one big RHTML file.

Thanks!
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-04-13 21:28
The Barge wrote:
> Jeff Coleman wrote:
>> There may be a better way to do it, but you could pass the name of the
>> partial to be rendered as a local variable to your "template" partial.
>>
>> render :partial => 'table', :locals => {:content_for_partial =>
>> 'admin_accounts'}
>>
>> _table.rhtml:
>>
>> <table border="0" cellpadding="0" cellspacing="0">
>> <tr><td class="admin_title"><%= the_title %></td></tr>
>> <tr><td class="admin_content"><%= render :partial => content_for_partial
>> %></td></tr>
>> </table>
>>
>> Again, there may be a better way, but this is at least one option
>> available.
>
> Sorry for the late reply, but this is working PERFECTLY.  I knew there
> had to be a way to do this so I could encapsulate different parts of the
> page so everything wasn't crammed into one big RHTML file.
>
> Thanks!

Glad it worked out for you!

Jeff Coleman
This topic is locked and can not be replied to.