Django templates, including page(s) that injects code into parent block

牧云@^-^@ 提交于 2019-12-24 11:28:52

问题


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

Code for base.html:

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

</script>

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>

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.

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_* %}.

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>

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?

Thanks!


回答1:


Try to use django-sekizai for that purpose.

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

{% render_block "js" %}

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

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

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



来源:https://stackoverflow.com/questions/32721155/django-templates-including-pages-that-injects-code-into-parent-block

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!