librelist archives

« back to archive

Several templates combined to one page

Several templates combined to one page

From:
Rune Christensen
Date:
2011-05-05 @ 05:21
Hi

I have a few days ago found the Flask framework and wanted to give it a
try.

I'm looking for a way to combine several templates into one web page. 

For example:

I want to have four functions/controllers that each lookup the data for
each area

header()
content()
sidebar()
footer()

and four templates

header.html
content.html
sidebar.html
footer.html

What are the best pratice way of solving this problem in Flask?

I was thinking about using signals to activate header(), sidebar() and
footer() after content() has been run. Then add blocks to the g variable
and at then end render the content.html template that extends
layout.html and includes the blocks saved in the g variable. I don't
know if that is possible?

Br.
Rune Christensen

Re: [flask] Several templates combined to one page

From:
Simon Sapin
Date:
2011-05-05 @ 06:12
Le 05/05/2011 07:21, Rune Christensen a écrit :
> Hi
>
> I have a few days ago found the Flask framework and wanted to give it a
> try.
>
> I'm looking for a way to combine several templates into one web page.
>
> For example:
>
> I want to have four functions/controllers that each lookup the data for
> each area
>
> header()
> content()
> sidebar()
> footer()
>
> and four templates
>
> header.html
> content.html
> sidebar.html
> footer.html
>
> What are the best pratice way of solving this problem in Flask?


Hi,

Generally each HTTP request is handled by one python view function and 
one Jinja template. Of course python functions can call other functions 
or classes, so you can organize your code any way you like. For example, 
have function for each area that return a dict of data. The "main" view 
function puts this together in the template.

Once you’re in a template, Jinja also has mechanisms to organize code 
and avoid repetition : {% extend %}, {% include %}, {% import %}, {% 
macro %}, maybe others ... Look at the Jinja docs. Your main template 
could use these mechanisms to call other templates and pass them 
relevant data.

> I was thinking about using signals to activate header(), sidebar() and
> footer() after content() has been run. Then add blocks to the g variable
> and at then end render the content.html template that extends
> layout.html and includes the blocks saved in the g variable. I don't
> know if that is possible?

What you describe is certainly possible, but I feel it adds much 
complexity compared to just calling functions/macros. As you said you 
still need a "master" template to put the pieces together.

Regards,
-- 
Simon Sapin
http://exyr.org/