librelist archives

« back to archive

Parent Jinja template cannot see Flask global object

Parent Jinja template cannot see Flask global object

From:
Амир Ахмадович Касимов
Date:
2013-03-16 @ 13:00
Hi,

My problem (I think) is actually with Jinja, but since probably Flask
guys are more familiar to this issue sent it here.

I have a `child.html` that extends `base.html`. The problem is I can't
use `g` in `base.html` until one block in `base.html` implemented by
`child.html`. Something like this:

base.html:
<head>
{% block head %}
<title>{% block title %}{% endblock %} | {{ 'Project' }}</title>
{% endblock head %}
</head>
<body>
{% block test %}{{ g.locale.name }}{% endblock %}

child.html:
{%extends "base.html"%}
{%block title%}{{ user['name'] }}{%endblock%}

`g.locale` is a dictionary containing information about current locale
set during application instance creation. Above code works fine, I
can't understand why this template fails:

base.html:
<html lang="{{ g.locale.name }}">
<head>
{% block head %}
<title>{% block title %}{% endblock %} | {{ 'Project' }}</title>
{% endblock head %}
</head>

Throwing exception:
	Jinja2.exceptions.UndefinedError
	UndefinedError: 'flask.ctx._AppCtxGlobals object' has no attribute 'locale'

But `g.locale` is available in first example. Am I using `g` in a wrong way?

Re: [flask] Parent Jinja template cannot see Flask global object

From:
Markus Unterwaditzer
Date:
2013-03-16 @ 13:35
"Амир Ахмадович Касимов" <ameretat.reith@gmail.com> wrote:

>Hi,
>
>My problem (I think) is actually with Jinja, but since probably Flask
>guys are more familiar to this issue sent it here.
>
>I have a `child.html` that extends `base.html`. The problem is I can't
>use `g` in `base.html` until one block in `base.html` implemented by
>`child.html`. Something like this:
>
>base.html:
><head>
>{% block head %}
><title>{% block title %}{% endblock %} | {{ 'Project' }}</title>
>{% endblock head %}
></head>
><body>
>{% block test %}{{ g.locale.name }}{% endblock %}
>
>child.html:
>{%extends "base.html"%}
>{%block title%}{{ user['name'] }}{%endblock%}
>
>`g.locale` is a dictionary containing information about current locale
>set during application instance creation. Above code works fine, I
>can't understand why this template fails:
>
>base.html:
><html lang="{{ g.locale.name }}">
><head>
>{% block head %}
><title>{% block title %}{% endblock %} | {{ 'Project' }}</title>
>{% endblock head %}
></head>
>
>Throwing exception:
>	Jinja2.exceptions.UndefinedError
>	UndefinedError: 'flask.ctx._AppCtxGlobals object' has no attribute
>'locale'
>
>But `g.locale` is available in first example. Am I using `g` in a wrong
>way?

I think you want to check out Jinja's globals. 
http://jinja.pocoo.org/docs/api/#the-global-namespace

You can access Jinja's env in Flask with app.jinja_env

-- Markus (from phone)

Re: [flask] Parent Jinja template cannot see Flask global object

From:
Ameretat Reith
Date:
2013-03-16 @ 20:52
> I think you want to check out Jinja's globals.
> http://jinja.pocoo.org/docs/api/#the-global-namespace
> 
> You can access Jinja's env in Flask with app.jinja_env
> 
> -- Markus (from phone)

Thanks Markus!

I couldn't fully understand cause of problem yet, but it works.