views:

28

answers:

1

I use jinja2 for my template engine in python.

i would like to join content of multiple block and would like to render it at the end of the template, just before tag. { they are various JavaScript snippets throughout the code in multiple template which i would like to move to the end of the file, how do i do it ? }

edit :

I would like to move all my inline javascript that are created in child jinja templates. I would like to move them to bottom of the page. so I have created a block in the parent template at the end of the page and using it in child template to write javascript. but , there may be multiple child, and so multiple javascript block, and as multiple block does not supported in jinja2 , what is the other solution do i have ? -------- one alternate i think is to create javascript itself in such a way that it does not need to be inline.

+1  A: 

I assume that by multiple children, you mean that there are templates inheriting from templates inheriting from templates ... inheriting from the base template. If that's the case, you need to define the same javascript block in each template and call super() in all of the children, in addition to adding more JavaScript. Calling super() prints the output of the parent's javascript block, and so on up the chain of inheritance. Along the way, each block may add code of its own.

So you could have something like this in each template:

{% block javascript %}
    {{ super() }}

    function foo(x, y) {
        return x + y;
    }
{% endblock %}
Nikhil Chelliah