15

I'm trying to implement django sekizai app. It is duplicating the js files that i'm adding.

base template:

{% load sekizai_tags %}
...
{% render_block "my_js" %}

template that is using this base:

{% load sekizai_tags %}
<div id="a1" >
    {% addtoblock "my_js" %}
        <script type="text/javascript" src="{{ MEDIA_URL }}js/my_js.js"></script>
    {% endaddtoblock %}
</div>
{% addtoblock "my_js" %}
    <script type="text/javascript" src="{{ MEDIA_URL }}js/my_js.js"></script>
{% endaddtoblock %}

Now here the rendered template has rendered twice.But when I tried adding the same script within the div it wasn't duplicated. Would appreciate if someone can shed some light on this!

Also when i try to use {% addtoblock %} in a template rendered by a template tag the script goes missing (It is neither included nor it stays in that template).

Note: The template tags, render_block and addtoblock, are from the django-sekizai package.

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
Vikas Gulati
  • 968
  • 2
  • 10
  • 24
  • well i could find the reason behind the duplication of my script which is the difference in the indentation of the script within and outside the div. {% addtoblock %} tag apparently doesn't remove the white-spaces. So the solution to my first question is a silly one as below: {% load sekizai_tags %}
    {% addtoblock "my_js" %}{% endaddtoblock %}
    {% addtoblock "my_js" %}{% endaddtoblock %}
    – Vikas Gulati Mar 21 '12 at 17:27
  • Also in custom template tag's template the scripts were disappearing. As per [django-sekizai-docs](https://github.com/ojii/django-sekizai/blob/master/docs/restrictions.rst) it is compulsory to use either SekizaiContext or RequestContext and the sekizai context processor but i found a workaround if you want to size down your template's context(say cont={}) use this cont['SEKIZAI_CONTENT_HOLDER'] = context['SEKIZAI_CONTENT_HOLDER']. With this workaround you need not use RequestContext and only sekezai context processor would be enough. – Vikas Gulati Mar 21 '12 at 19:54

2 Answers2

29

{% addtoblock %} and {% endaddtoblock %} have to be inside of a block in templates that inherit another template.

# base.html
<html>
    ...
    {% render_block 'js' %}
    {% block js %}{% endblock %}
</html>


# some-page.html
{% inherits 'base.html' %}

{% block js %}
    {% addtoblock 'js' %}
        <script type="text/javascript" ... />
    {% endaddtoblock %}
{% endblock %}

Hope that helps you out.

Brandon Taylor
  • 33,823
  • 15
  • 104
  • 144
7

{% addtoblock %} inside the template (something.html) from an inclusion tag:

from django import template
from django.conf import settings

register = template.Library()

@register.inclusion_tag('something.html', takes_context=True)
def render_something(context, some_arg):
    sezikai_ctx_var = getattr(settings, 'SEKIZAI_VARNAME', 'SEKIZAI_CONTENT_HOLDER')
    attrs = {
        'some_arg': some_arg,
        sezikai_ctx_var: context[sezikai_ctx_var]
    }
    return attrs
laffuste
  • 16,287
  • 8
  • 84
  • 91