有没有办法将变量传递给Jinja2父母? [英] Is there a way to pass variables into Jinja2 parents?

查看:69
本文介绍了有没有办法将变量传递给Jinja2父母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

模板:

<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"传递给父对象,有没有办法做到这一点?

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

提前谢谢!

推荐答案

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

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

另请参阅以下问题:覆盖应用程序引擎模板如果在

这篇关于有没有办法将变量传递给Jinja2父母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆