django:在包含的模板中使用块 [英] django: using blocks in included templates

查看:163
本文介绍了django:在包含的模板中使用块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些html结构在不同的地方重复使用。它与整个模板不同,所以我无法扩展它,它也可以用于包含复杂的内容,所以我不认为将其定义为模板标签做得很好。以下是一些描述我想要的结果的伪代码,当使用template_level2.html时,可以通过调用其中的块轻松地将东西放入reusable_pattern_template。如果我使用这个代码,你在template_level_2.html的实际内容中写的内容将不会显示出来。我该怎么处理这个?



base.html

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

template_level1.html

  {%extends'base.html'%} 
{%block content%}
这里有东西
{%include'reusable_pattern_template.html'%}
这里有其他东西...
{%endblock%}

reusable_pattern_template.html

 < div> 
< div>
< div>
{%block local_content%} {%endblock%}
< / div>
< / div>
< / div>

template_level2.html

  {%extends'template_level1.html'%} 
{%block local_content%}
此处的实际内容...
{%endblock%}

更新:
对不起,template_level2.html中的扩展有一些拼写错误,我们只是更正了它。



可能不是很清楚,但上面的代码更多的是描述我想要的结果的伪代码。简而言之,




  • 我想在我的
    模板中加入可重复使用的html模式的小块。

  • 这些模式就像框,你可以在其中放入整个html
    的内容。因此,上下文变量可能对于
    我的目的有点太限制。


解决方案

Django不处理包含的文件中的块。


include标签应被视为渲染此子模板并包含HTML ,而不是解析这个子模板,并将其内容包含为父类的一部分。这意味着在包含的模板之间没有共享状态 - 每个include都是一个完全独立的渲染过程。 ( Django模板标记文档



I have some html structures that are reused in quite a few different places. It's different from a overall template so I can't extend it, it can also be used to contain complicated content so I don't think defining it as a template tag does a good job. Below is some pseudo code describing my desired outcome, when using template_level2.html you can easily put stuff into the reusable_pattern_template by calling the block inside it. If I do use this code, what you write in 'actual content' of template_level_2.html wouldn't show up. How should I deal with this?

base.html

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

template_level1.html

{% extends 'base.html' %}
{% block content %}
  Something here...
  {% include 'reusable_pattern_template.html' %}
  Something else here...
{% endblock %}

reusable_pattern_template.html

<div>
  <div>
    <div>
      {% block local_content %}{% endblock %}
    </div>
   </div>
</div>

template_level2.html

{% extends 'template_level1.html' %}
{% block local_content %}
  Actual content here...
{% endblock %}

update: Sorry, the extends in template_level2.html has some misspelling, I've just corrected it.

It may not be very clear, but the code above is more of a pseudo code describing my desired outcome. In short,

  • I would like to include small pieces of reusable html patterns in my templates.
  • These patterns are like boxes, that you can put whole pieces of html content in them. So context variables may be a bit of too limited for my purpose

解决方案

Django does not process blocks in included files.

The include tag should be considered as an implementation of "render this subtemplate and include the HTML", not as "parse this subtemplate and include its contents as if it were part of the parent". This means that there is no shared state between included templates -- each include is a completely independent rendering process. (Django template tag documentation)

这篇关于django:在包含的模板中使用块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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