librelist archives

« back to archive

Cannot access global object in parent template

Cannot access global object in parent template

From:
Ameretat Reith
Date:
2013-03-16 @ 09:46
Hi,
My problem is (i think) actually with Jinja but i post it here since
probably Flask guys had this issue before or can suggest me better
design.

I'm trying to extend `base.html` with `child.html`. `base.html` should
see Flask `g` object. But I couldn't achieve this until one block in
`base.html` extended by `child.html`. Something like this:

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

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

I'm setting a dictionary in `g` (during application instantiation) that
stores current locale settings. Above code works perfectly but this
code not:

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

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

I can pass `g` on `render_template` call but looking for more mature
methodology. Am I using `g` in templates in a wrong way?

Thanks in advance and sorry for poor English.