# Template "specialisation"

**URL:** <https://www.ruby-forum.com/t/template-specialisation/67850>\
**Category:** Rails\
**Created:** [September 7, 2006, 9:34am UTC](https://www.ruby-forum.com/t/template-specialisation/67850 "2006-09-07T09:34:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [September 7, 2006, 9:34am UTC](https://www.ruby-forum.com/t/template-specialisation/67850/1 "2006-09-07T09:34:14Z")

</div>

I want to use a sitewide default template and use a specific version in  
some  
controllers to have a good DRY approach to my layout. Those controllers  
should add a little bit more HTML before rendering the results of the  
actions, in order to avoid copying the entire sitelayout every time.  
How can I do that? Currently my code is like this:

class WelcomeController \< ApplicationController  
#before\_filter :show\_options  
layout ‘mydefault’

```
    def contact
            render :partial => "contact"
    end

    private
            def show_options
                    render :partial => "welcome"
            end

```

end

This only shows the app/views/welcome/\_contact.rhtml page. It does not  
show  
the layout (that does work in other controllers where I don’t use these  
things). If I then enable the before\_filter (is that the right usage, it  
does not feel like that) then it only shows the  
app/views/welcome/\_welcome.rhtml page, which is not what I want either.

So how should I do this? How should I have this controller use the  
layout ‘mydefault’, render the \_welcome partial before every method and  
also render the contact partial when the contact action is called?

I really think I’m not using the right tools here, especially  
before\_filter  
seems a bit wrong here.

Thanks for your help,  
Bart

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [September 7, 2006, 10:10am UTC](https://www.ruby-forum.com/t/template-specialisation/67850/2 "2006-09-07T10:10:17Z")

</div>

Hi Bart,

The way I would do it is to get rid of the partials from your controller  
and put them in the views. Keep the layout ‘mydefault’ at the top of  
your WelcomeController as you have it. Your ‘mydefault’ layout should  
look something like:

header stuff…  
\<%= render :partial =\> “welcome” %\>  
\<%= @content\_for\_layout %\>  
footer stuff…

The above layout will always render the “welcome” partial for all  
actions. You don’t need the before filter. For your contact view, you  
can include whatever you want in its .rhtml view file. So contact.rhtml  
can have this in it:  
\<% render :partial =\> “contact” %\>

I’m not sure why you would even need the partial. Just put the contents  
directly in contact.rhtml in your views folder.

Best Regards,

Tamim

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [September 7, 2006, 12:20pm UTC](https://www.ruby-forum.com/t/template-specialisation/67850/3 "2006-09-07T12:20:25Z")

</div>

Hi Tamim (and thanks for your fast reply),

Indeed I should have put the contact stuff in contact.rhtml. But… the  
mydefault layout is also used for controllers that do not have any  
welcome  
information to show. Wouldn’t you solution show the welcome partial  
everywhere?

Regards and thanks,  
Bart

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [September 7, 2006, 9:47pm UTC](https://www.ruby-forum.com/t/template-specialisation/67850/4 "2006-09-07T21:47:57Z")

</div>

Hi Bart,

From your message, I got the impression that you wanted the \_welcome  
partial to be rendered before every method:

“So how should I do this? How should I have this controller use the  
layout ‘mydefault’, render the \_welcome partial before every method and  
also render the contact partial when the contact action is called?”

Another possible way that could work is if you create a simple private  
method that does this

private  
def show\_welcome  
@welcome = true  
end

Then at the top, you can run your before filter

before\_filter :show\_welcome

Then in ‘mydefault’:

header stuff…  
\<% if @welcome == true %\>  
\<%= render :partial =\> “welcome” %\>  
\<% end %\>  
\<%= @content\_for\_layout %\>  
footer stuff…

There’s probably a better way to do it, but that’s my brainstorming at  
the moment.

Best Regards,

Tamim

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [September 7, 2006, 11:48pm UTC](https://www.ruby-forum.com/t/template-specialisation/67850/5 "2006-09-07T23:48:04Z")

</div>

You can also check which controller and/or action you’re in from the  
view and display appropriately. For instance, for all actions in  
controllers one, two, and three, show the welcome:

\<% if params[:controller] =~ /one|two|three/ %\>  
\<%= render :partial =\> “welcome” %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [September 8, 2006, 9:15am UTC](https://www.ruby-forum.com/t/template-specialisation/67850/6 "2006-09-08T09:15:21Z")

</div>

Tamim A. wrote:

> There’s probably a better way to do it, but that’s my brainstorming at the  
> moment.

Your suggestion works, thanks!  
And sorry if my questions were not entirely clear all the time…

Regards,  
Bart

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [September 8, 2006, 9:09am UTC](https://www.ruby-forum.com/t/template-specialisation/67850/7 "2006-09-08T09:09:18Z")

</div>

Curtis S. wrote:

> You can also check which controller and/or action you’re in from the  
> view and display appropriately. For instance, for all actions in  
> controllers one, two, and three, show the welcome:
> 
> \<% if params[:controller] =~ /one|two|three/ %\>  
> \<%= render :partial =\> “welcome” %\>  
> \<% end %\>

That seems like a good trick! I’ll try to use that one.

Regards,  
Bart
