Topic: How to load data for footer and other common blocks on site?

Hi guys,

I write Django application, but asking here, because RoR have reputation of the etalon MVC framework.

Many pages on my site have common blocks: footer, sidebar, userblock.
Some of them (sidebar for instance) depends on the page being displayed.
I use inclusion (and inheritence) of templates to avoid html code duplication,
but how do i load data for sub-templates ?

I see three solutions:
1) Load data directly from sub-templates, inserting code there. It simple, but make templates *active*.
2) Write helper functions, such as *load_data_for_footer*, and call them from each controller.
3) Use some kind of controller hierarchy. May be inheritence. So root controller will load data for footer,
and each its child will get this data in context automaticaly. But Django lacks of such features =(

What is the best solution from RoR's point of view ?

Thank you and sorry for my poor english.

Last edited by mialinx (2011-10-06 02:55:27)

Re: How to load data for footer and other common blocks on site?

What about partials?

WikiBook wrote:

If you have a lot of HTML code that repeats itself in many different views (such as forms, dynamic menus,...) you might want to consider using partials: With partials, you just move the code that you use often into a separate file and place a link inside the files that should use your partials...Overall partials are a very good way to minimize the effort of copying the same code over and over, but to keep the needed code in single files that can be altered easily and used as often as you want to.

WikiBooks link

Re: How to load data for footer and other common blocks on site?

ToddG wrote:

What about partials?

WikiBook wrote:

If you have a lot of HTML code that repeats itself in many different views (such as forms, dynamic menus,...) you might want to consider using partials: With partials, you just move the code that you use often into a separate file and place a link inside the files that should use your partials...Overall partials are a very good way to minimize the effort of copying the same code over and over, but to keep the needed code in single files that can be altered easily and used as often as you want to.

WikiBooks link

As far as i understood , partials is just templates for repeatedly used html pieces.
But how do i load data for them ?

Re: How to load data for footer and other common blocks on site?

I don't have much experience working with partials, but it looks like there are at least two ways to load data for them.

You can pass data from the parent view to the partial. 

# partial_test.rhtml
<%= render :partial => "my_partial", :locals => { :parent_msg => "Hello from Parent View"} %>

#_my_partial.rhtml
<%= parent_msg %>

Or you can pass data from the View's public method in the controller:

class SiteController < ApplicationController
    def partial_test
        @controller_msg = "Hello from controller"
    end
end

#_my_partial.rhtml
<%= @controller_msg %>

But apparently you can't create a public method for the partial, as neither one of these worked for me:

class SiteController < ApplicationController
    def _my_partial
        @partial_msg = "Hello from partial"
    end

    def my_partial
        @partial_msg = "Hello from partial"
    end
end

#_my_partial.rhtml
<%= @partial_msg %>

Last edited by ToddG (2011-10-10 08:53:23)

Re: How to load data for footer and other common blocks on site?

Not sure if this would help, but you could take a look into using cells. (http://cells.rubyforge.org/)

Re: How to load data for footer and other common blocks on site?

freezzo wrote:

Not sure if this would help, but you could take a look into using cells. (http://cells.rubyforge.org/)

Cells seem to be a nice solution.. I'll try to find analogue in django.
Thank!