有没有办法将变量传递给Jinja2父母?


问题内容

我正在尝试将某些变量从子页面传递到模板。这是我的python代码:

    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))

模板:

<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>

和孩子:

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

问题是,我想将变量“ trying”传递给父对象,有没有办法做到这一点?

提前致谢!


问题答案:

我不明白你的问题。当您将变量传递给上下文时(与尝试一样),这些变量将在子代和父代中可用。要将标题传递给父项,您必须使用继承,有时还要与super结合使用:http : //jinja.pocoo.org/docs/templates/#super-
blocks

另请参阅以下问题:在if中覆盖应用引擎模板块