ihsoy ih
ihsoy ih

Reputation: 1018

Is there a way to pass variables into Jinja2 parents?

I'm trying to pass some variables from the child page to the template. This is my python code:

    if self.request.url.find("&try") == 1:
        isTrying = False
    else:
        isTrying = True

    page_values = {
        "trying": isTrying
    }

    page = jinja_environment.get_template("p/index.html")
    self.response.out.write(page.render(page_values))

The template:

<html>
  <head>
    <link type="text/css" rel="stylesheet" href="/css/template.css"></link>
    <title>{{ title }} | SST QA</title>

    <script src="/js/jquery.min.js"></script>

  {% block head %}{% endblock head %}
  </head>
  <body>
    {% if not trying %}
    <script type="text/javascript">
    // Redirects user to maintainence page
    window.location.href = "construct"
    </script>
    {% endif %}

    {% block content %}{% endblock content %}
  </body>
</html>

and the child:

{% extends "/templates/template.html" %}
{% set title = "Welcome" %}
{% block head %}
{% endblock head %}
{% block content %}
{% endblock content %}

The problem is, I want to pass the variable "trying" into the parent, is there a way to do this?

Thanks in advance!

Upvotes: 22

Views: 19351

Answers (4)

user1876508
user1876508

Reputation: 13172

The example on the Jinja2 Tips and Tricks page explains this perfectly, http://jinja.pocoo.org/docs/templates/#base-template. Essentially, if you have a base template

**base.html**
<html>
    <head>
        <title> MegaCorp -{% block title %}{% endblock %}</title>
    </head>
    <body>
        <div id="content">{% block content %}{% endblock %}</div>
    </body>
</html>

and a child template

**child.html**
{% extends "base.html" %}
{% block title %} Home page {% endblock %}
{% block content %}
... stuff here
{% endblock %}

whatever python function calls render_template("child.html") will return the html page

**Rendered Page**
<html>
    <head>
        <title> MegaCorp - Home page </title>
    </head>
    <body>
        <div id="content">
            stuff here...
        </div>
    </body>
</html>

Upvotes: 17

sscalvo
sscalvo

Reputation: 475

You just need to declare that variable before extending the template, so the extended template will have access to to the variable trying

{% set trying = True %}  <----------- declare variable

{% extends "/templates/template.html" %}
{% set title = "Welcome" %}
{% block head %}
{% endblock head %}
{% block content %}
{% endblock content %}

Few years later but hoping it may help latecomers

Upvotes: 8

Dimitar Marinov
Dimitar Marinov

Reputation: 942

I think that you are looking to highlight active menus in the base layout and you need something like this

{% extends 'base.html' %}
{% set active = "clients" %}

then use can use "active" inside base.html

Upvotes: 9

voscausa
voscausa

Reputation: 11706

I do not understand your problem. When you pass variables to the context (as you do with trying) these variables will be available in the child and the parent. To pass title to the parent, you have to use inheritance, sometimes in combination with super : http://jinja.pocoo.org/docs/templates/#super-blocks

See also this question: Overriding app engine template block inside an if

Upvotes: 2

Related Questions