Tuesday, 15 March 2011

python - Jinja2 variables in included template -



python - Jinja2 variables in included template -

i have parent template settings variable , template includes couple of other sub-templates want reuse variable. unfortunately in children templates variable empty. how prepare it?

<!-- index.html --> {%- set title= ' :: '.join((caption, page_title or '')) -%} {%- set description= ' :: '.join((desc, meta_desc or '')) -%} {%- block page_header -%} {% include 'parts/_header.html' %} <!-- tried context without luck --> {% include 'parts/_header.html' %} {%- endblock page_header -%} <!-- parts/header.html --> <header class="header-wrapper"> <div class="header"> <div class="title"><h1 class="title">{{ title|safe }}</h1></div> <div class="description">{{ description|safe }}</div> </div> </header>

upd: after little research found issue related {% block %} section - in include tag outside block provided document context. reasons i'd maintain within block.

in experience, can fixed including {% set %} declarations within {% block %} declarations. in example:

<!-- index.html --> {%- block page_header -%} {%- set title= ' :: '.join((caption, page_title or '')) -%} {%- set description= ' :: '.join((desc, meta_desc or '')) -%} {% include 'parts/_header.html' %} {%- endblock page_header -%}

if need set same variables outside block, set same line twice, 1 time outside block , 1 time inside. seems imperfect me, don't know way.

python jinja2

No comments:

Post a Comment