Re: [flask] macros in flask
- From:
- Matthew Frazier
- Date:
- 2011-02-18 @ 12:33
On 02/18/2011 04:31 AM, srinivas hn wrote:
> Hi ,
>
> I am new to the flask framework i wanted to know is there any concepts
> like macros in flask so that i can use macro where ever i need ??
>
> If so please provide me some pointers fo it
"Macro" can mean a lot of different things. Are you referring to
template macros? Lisp-style macros that modify the code?
Jinja2 does have macros, which let you define sections of template code
and reuse them by importing them from other templates. For example, if
_helpers.html contains:
{% macro display_post(post) %}
<h3>{{ post.title }}</h3>
<p>{{ post.text }}</p>
{% endmacro %}
Then in another template, you can have:
{% from "_helpers.html" import display_post %}
{% for post in posts %}
{{ display_post(post) }}
{% endfor %}
And it will display each post with a heading and a paragraph, like you
defined in your macro. You can also access macros from Python code using
the get_template_attribute function - check the docs for more
information on that.
> CHEERS
> CNA
> 9986229891
--
Regards, Matthew Frazier