Advice on Similar Views with Helpers


#1

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? :slight_smile:

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…

<%= the_title %>
<%= @content_for_partial %>

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.


#2

The B. 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? :slight_smile:

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…

<%= the_title %>
<%= @content_for_partial %>

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

<%= the_title %>
<%= content %>

Jeff


#3

Jeff C.man 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

<%= the_title %>
<%= content %>

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.


#4

The B. wrote:

Jeff C.man 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

<%= the_title %>
<%= content %>

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


#5

Jeff C.man wrote:

The B. wrote:

Jeff C.man 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

<%= the_title %>
<%= content %>

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 :slight_smile: The solution is probably
staring me right in the face.


#6

Jeff C.man wrote:

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

_table.rhtml:

<%= the_title %>
<%= render :partial => content_for_partial %>

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 C.man


#7

The B. wrote:

Jeff C.man wrote:

The B. wrote:

Jeff C.man 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

<%= the_title %>
<%= content %>

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 :slight_smile: 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:

<%= the_title %>
<%= render :partial => content_for_partial %>

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

Jeff C.man


#8

The B. wrote:

Jeff C.man 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:

<%= the_title %>
<%= render :partial => content_for_partial %>

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 C.man


#9

Jeff C.man 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:

<%= the_title %>
<%= render :partial => content_for_partial %>

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!