Django模板,包括将代码注入父区块的页面 [英] Django templates, including page(s) that injects code into parent block

查看:129
本文介绍了Django模板,包括将代码注入父区块的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在另一个模板中包含一个模板(包含 include django模板标签),并通过 block 标签,或类似的东西?

Is it possible to include a template (with include django template tag) within another template and "inject" some content to the page that includes (parent) through block tag, or something similar?

假设我的项目中有以下文件结构:

Let's say I have the following file structure within my project:

App/
    (...)
    templates/
        base.html
        index.html
        _include1.html
        _include2.html
        _include3.html
        _include4.html

代码 base.html

<!DOCTYPE html>
<html>
<head lang="en">
    (...)
</head>
<body>
<script type="application/javascript">
    $(function () {
        {% block extra_script %}
        {% endblock %}
    });

</script>

代码 index.html



Code for index.html:

{% extends "base.html" %}
{% load static %}
(...)
<div class="row gutter">
    <div>
        {% include "_include1.html" with object=object%}
    </div>
    <div>
        {% include "_include2.html" with object=object %}
    </div>
    <div>
        {% include "_include3.html" with object=object %}
    </div>
    <div>
        {% include "_include4.html" with object=object %}
    </div>
</div>

在每个 _include * .html 想要调用一些特定的JS函数(例如),但是我想把它放在父级( index.html base.html ,在我的情况下无关紧要) extra_script block。我在文档中搜索了其他问题,并没有找到一种方法来执行此操作与 include 语法。

And in each _include*.html I would like to call some specific JS function (for example), but I want to place it in the parents (index.html or base.html, doesn't matter in my case) extra_script block. I searched in the documentation, other questions and didn't find a way to do this with the include syntax.

我已经做了类似的事情,但通过扩展标签。但是,我不想在每个页面的 index.html base.html 中定义一个块需要包含( {%bloc include_ *%}

I've done something similar but through extends tag. However I don't want to define a block in the index.html or base.html for each page that I need to include ({% bloc include_* %}.

所以我现在的解决方案)是在这样的每个包含的页面中定义一个脚本( _include1.html ):

So the solution that I have now (and works) is to define a script in each included page like this (_include1.html):

<div>
(...)
</div>
<script>
    $(function () {
        //Code that should be placed within parents script page (this is just an example)
        var a = function (){
                    (...)
                };
        a();
    });
</script>

但是我认为有一个更好的方式来做,通过使用django模板引擎,而不必为每个页面定义一个块需要包括在内。另外我想把我所有的js代码放在一个地方(父母< script> 标签),而不是分散所有的地方(就像现在的解决方案一样)。

However I think there's a better way to do this, by making use of django templates engine, and without having to define a block for each page that needs to be included. Also I would like to have all my js code in a single place (parents <script> tag) instead of being scattered all over the place (like it is with the presented solution).

任何人都可以给出一些投入或想法?

Can anyone give some input or ideas towards this?

谢谢!

推荐答案

尝试使用 django-sekizai 为此目的。

使用sekizai,您可以在之前定义一个JavaScript块< / body>

With sekizai, you can define a JavaScript block just before the </body>:

{% render_block "js" %}

然后每当你需要添加JavaScript到该块,你写这个:

And then whenever you need to add JavaScript to that block, you write this:

{% addtoblock "js" %}
    <script type="text/javascript">
        // your JavaScript
    </script>
{% endaddtoblock %}

如果{%addtoblock中的内容重复%}块,它们将仅使用一次。

If there are duplicates of the content in the {% addtoblock %} blocks, they will be used only once.

这篇关于Django模板,包括将代码注入父区块的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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