【问题标题】:Jinja2 is duplicating content when extendsJinja2 扩展时复制内容
【发布时间】:2014-09-10 05:11:40
【问题描述】:

当 index.html 继承自 base.html Jinja2 复制内容时,我遇到了问题。

我正在使用来自http://jinja.pocoo.org/docs/dev/templates/#template-inheritance 的示例代码,但浏览器中的内容完全重复。我不知道是不是因为 jinja 环境设置错误,扩展标签或类似的东西。

base.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
{% block head %}
<link rel="stylesheet" href="style.css" />
<title>{% block title %}{% endblock %} - My Webpage</title>
{% endblock %}
</head>
<body>
<div id="content">{% block content %}{% endblock %}</div>
<div id="footer">
{% block footer %}
  &copy; Copyright 2008 by <a href="http://domain.invalid/">you</a>.
{% endblock %}
</div>
</body>

index.html

{% extends "base.html" %}
{% block title %}Index{% endblock %}
{% block head %}
{{ super() }}
<style type="text/css">
    .important { color: #336699; }
</style>
{% endblock %}
{% block content %}
<h1>Index</h1>
<p class="important">
  Welcome on my awesome homepage.
</p>
{% endblock %}

这是我得到的(相同的 html 代码两次):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="style.css" />
<title>Index - My Webpage</title>
<style type="text/css">
    .important { color: #336699; }
</style>
</head>
<body>
<div id="content">
<h1>Index</h1>
<p class="important">
  Welcome on my awesome homepage.
</p>
</div>
<div id="footer">
&copy; Copyright 2008 by <a href="http://domain.invalid/">you</a>.
</div>
</body>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>    
<link rel="stylesheet" href="style.css" />
<title>Index - My Webpage</title>
<style type="text/css">
    .important { color: #336699; }
</style>
</head>
<body>
<div id="content">
<h1>Index</h1>
<p class="important">
  Welcome on my awesome homepage.
</p> 
</div>
<div id="footer">

    &copy; Copyright 2008 by <a href="http://domain.invalid/">you</a>.
</div>
</body>

有什么想法吗?非常感谢。

更新!!

这是我的处理程序。我正在使用 webapp2。

class LandingHandler(webapp2.RequestHandler):
    def get(self):
        template = settings.JINJA_ENVIRONMENT.get_template('index.html')
        self.response.write(template.render(dict()))

更新 2!!

在我的 settings.py 中

JINJA_ENVIRONMENT = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__),'templates')),
    extensions=['jinja2.ext.autoescape'],
    autoescape=True)

【问题讨论】:

  • 你能发布你的 Jinja 调用代码来渲染和输出结果吗?乍一看,我看不出模板有什么问题。
  • 丹,谢谢。我已经更新了帖子。
  • index.html 中{{ super() }} 标记的用途是什么?会不会是这个原因?
  • 我还没有使用过扩展,所以对我的 cmets 持保留态度。我注意到在基本模板中,标题块嵌套在头块中,但不在 index.html 模板中。
  • super() 插入当前块的内容,因为它出现在父模板中,在这种情况下应该只是 &lt;link&gt;&lt;title&gt; 元素(和 title 块) .

标签: python google-app-engine jinja2


【解决方案1】:

Jinja(和 Bootstrap)有点奇怪。在发出警告之前,它也会考虑 html cmets。因此,如果您在该文件上有任何 cmets,可能是注释掉的 {% block content %} ... {% endblock %} 部分,然后将其从那里完全删除,它将起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多