librelist archives

« back to archive

flask jinja2 g undefined

flask jinja2 g undefined

From:
Alex K
Date:
2011-11-11 @ 08:00
I created many jinja2 macros in macros.html file and imported him to
templates via

{% import "macro.html" as j %}

and then call them as

{{ j.render_login_form(login_form) }}

etc...

and in them I can use g.object like g.user, etc.
but in one macros g is undefined, and I can't understand why.

How can I debug this?

I have 2 macroses for comments:
{% macro comment_render_body(comment, cur_level, prev_level, loop_last,
is_author) -%}
{% include 'mods/blog/tags/comment_render_body.html' with context %}
{%- endmacro %}

{% macro comments_render(comments, is_author=False) -%}
{% include 'mods/blog/tags/comments_render.html' with context %}
{%- endmacro %}

in mods/blog/tags/comments_render.html:

{% import "macro.html" as j %}

    {% if comments %}
                {% set prev_level = None %}

                {% for comment in comments %}
                    {% set cur_level = comment.level %}
                    {% set loop_last = loop.last %}

                    {{ j.comment_render_body(comment, cur_level,
prev_level, loop_last, is_author) }}

                    {% set prev_level = comment.level %}
                {% endfor %}

        </section>
    {% endif %}


and in comments_render g is available, but in comment_render_body.html g is
undefined and I can't use g.user.


comment_render_body.html:

{% import "macro.html" as j %}

{{- comment.text -}}





How I can fix this?


Thanks!

Re: [flask] flask jinja2 g undefined

From:
Lix Xu
Date:
2011-11-11 @ 09:30
try this
{% import "macro.html" as j with context %}

On Fri, Nov 11, 2011 at 16:00, Alex K <lestatcheb@googlemail.com> wrote:

> I created many jinja2 macros in macros.html file and imported him to
> templates via
>
> {% import "macro.html" as j %}
>
> and then call them as
>
> {{ j.render_login_form(login_form) }}
>
> etc...
>
> and in them I can use g.object like g.user, etc.
> but in one macros g is undefined, and I can't understand why.
>
> How can I debug this?
>
> I have 2 macroses for comments:
> {% macro comment_render_body(comment, cur_level, prev_level, loop_last,
> is_author) -%}
> {% include 'mods/blog/tags/comment_render_body.html' with context %}
> {%- endmacro %}
>
> {% macro comments_render(comments, is_author=False) -%}
> {% include 'mods/blog/tags/comments_render.html' with context %}
> {%- endmacro %}
>
> in mods/blog/tags/comments_render.html:
>
> {% import "macro.html" as j %}
>
>     {% if comments %}
>                 {% set prev_level = None %}
>
>                 {% for comment in comments %}
>                     {% set cur_level = comment.level %}
>                     {% set loop_last = loop.last %}
>
>                     {{ j.comment_render_body(comment, cur_level,
> prev_level, loop_last, is_author) }}
>
>                     {% set prev_level = comment.level %}
>                 {% endfor %}
>
>         </section>
>     {% endif %}
>
>
> and in comments_render g is available, but in comment_render_body.html g
> is undefined and I can't use g.user.
>
>
> comment_render_body.html:
>
> {% import "macro.html" as j %}
>
> {{- comment.text -}}
>
>
>
>
>
> How I can fix this?
>
>
> Thanks!
>
>
>
>
>
>

Re: [flask] flask jinja2 g undefined

From:
Alex K
Date:
2011-11-11 @ 09:50
Thanks! It works!


On Fri, Nov 11, 2011 at 1:30 PM, Lix Xu <xuzenglin@gmail.com> wrote:

> try this
> {% import "macro.html" as j with context %}
>
>

Re: [flask] flask jinja2 g undefined

From:
Armin Ronacher
Date:
2011-11-11 @ 14:03
Hi,

On 11/11/11 10:50 AM, Alex K wrote:
> Thanks! It works!
Keep in mind that this invalidates template caching (with context).
Instead I would recommend passing g to the macro.


Regards,
Armin