嵌套的Django模板 [英] Nested django templates

查看:76
本文介绍了嵌套的Django模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一件非常基本的事情,但是尽管我已经使用Django大约一年了,但我还从未遇到过这种情况.

This seems like a pretty basic thing to do but although I've been using Django for around a year, I've never run into this scenario yet.

在许多模板/Web框架中,模板继承的工作方式略有不同,因为它的行为通常更像包装器,因此,如果您有childtemplate.html,parenttemplate.html和grandparenttemplate.html,则通常会进行最终渲染看起来像这样:

In a lot of templating/web frameworks, template inheritance works a bit differently, in that usually it behaves more like wrappers, so if you have childtemplate.html, parenttemplate.html, and grandparenttemplate.html, then the finally rendering usually looks something like:

grandparent header
    parent header
        child header
        child content
    parent content
    parent footer
grandparent content
grandparent footer

这不是它在Django中的确切工作方式,但我想知道如何实现它.

That's not exactly how it works in Django but I'm wondering how to implement it.

具体地说,我有我的子"模板,我们只说它是foo.html. foo.html可选获取变量parent_template或默认为"base.html"

Specifically I have my "child" template, let's just say it's foo.html. foo.html optional gets a variable parent_template or defaults to "base.html"

{% extends parent_template|default:"base.html" %}

{% block content %}
I am a foo and my title is {{ foo.title }}
{% endblock content %}

所以,这就是我遇到的障碍.如果parent_template是模板,则该模板应包装foo.html的内容,然后将结果放入base.html:

So, this is where I run into a snag. If parent_template is a template, that template should wrap the contents of foo.html and then place the result into base.html:

{% extends "base.html" %}

{% something_magical_here %}
<div>parent header</div>
# content of foo.html
<div>parent footer</div>
{% end_something_magical_here %}

然后在base.html中:

<html>
... snip ...
<div id="content">
{% something_else_magical %}
# content of parent_template rendering, or just foo.html if no parent_template given
{% end_something_else_magical %}

应呈现为

<html>
... snip ...
<div id="content">
<div>parent header</div> 
I am a foo and my title is Bar
<div>parent footer</div>

如果已设置parent_template并且

if parent_template is set and

<html>
... snip ...
<div id="content">
I am a foo and my title is Bar

如果不是.

我希望我的问题很清楚:我需要(可选)将模板包装在父模板中,然后将其结果发送到base.html模板.

I hope my problem is clear: I need to (optionally) wrap a template in a parent template, and then send the results of that to the base.html template.

通常,这样的事情可能会起作用:

Normally, something like this might work:

#foo.html
{% extends "parent.html" %}
{% block child_content %}
I am a foo and my title is {{ foo.title }}
{% endblock child_content %}

#parent.html
{% extends "base.html" %}

{% block content %}
parent header
{% block child_content %}{% endblock child_content %}
parent content
parent footer

#base.html
base header
{% block content %}{% endblock content %}
base content
base footer

但是,由于parent_template可以为空,所以有时base.html只会得到child_content块,而不是content块.

However, since the parent_template could be blank, then sometimes base.html will be getting just the child_content block, not the content block.

此外,我希望能够执行此操作而不必创建一堆子块(如果我决定foo应用程序应具有自己的/foo/base.html然后调用/base.html的话) ?

Also, I'd like to be able to do this without having to create a bunch of sub-blocks (what if I decide that the foo application should have its own /foo/base.html that then calls /base.html)?

有什么想法吗?

推荐答案

extends模板标记可以采用可变参数.

the extends template tag can take a variable argument.

如此:

base.html
    {% block content %}
        <p>BASE</p>
    {% endblock %}

parent.html
    {% extends "base.html" %}

    {% block content %}
        {{ block.super }}
        <p>PARENT</p>
    {% endblock %}

foo.html
    {% extends ext_templ %}

    {% block content %}
        {{ block.super }}
        <p>FOO</p>
    {% endblock %}

使用基数:

return render_to_response('foo.html', {'ext_templ':'base.html'})

给您

<p>BASE</p>
<p>FOO</p>

使用父级:

return render_to_response('foo.html', {'ext_templ':'parent.html'})

给您

<p>BASE</p>
<p>PARENT</p>
<p>FOO</p>

解决此问题以获取嵌套块的一种方法是:

one way around this problem to get nested blocks is:

base.html
    {% block content %}
        {% block top %}
            <p>BASE START</p>
        {% endblock %}

        {% block bot %}
            <p>BASE END</p>
        {% endblock %}
    {% endblock %}


parent.html
    {% extends "base.html" %}

    {% block top %}
        {{ block.super }}
        <p>PARENT</p>
    {% endblock %}

    {% block bot %}
        <p>PARENT</p>
        {{ block.super }}
    {% endblock %}

foo.html
    {% extends ext_templ %}

    {% block top %}
        {{ block.super }}
        <p>FOO</p>
    {% endblock %}

    {% block bot %}
        <p>FOO END</p>
        {{ block.super }}
    {% endblock %}

我能想到的另一种方法是用{% if ext_templ == 'parent.html' %}标签包装块,但看起来并不干燥.我很好奇看到其他人对嵌套块也有反应.

The other method that i can think of is to wrap the blocks with an {% if ext_templ == 'parent.html' %} tag but that doesn't seem very dry. I'm curious to see other peoples response to nested blocks as well.

这篇关于嵌套的Django模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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