8

I am working on a DjangoCMS plugin, which includes a javascript file for itself. The plugin's Javascript relies on the same libraries that the rest of the site does. So, here's the conceptual part of what I have right now:

Basetemplate.html

{% load cms_tags sekizai_tags and_a_bunch_of_other_stuff %}
<html>
...
<head>
{% render_block "css" %}
</head>
<body>
...
{% addtoblock "js" %}[jquery]{% endaddtoblock %}
{% addtoblock "js" %}[google api, data, more cool stuff like jqplot.]{%endaddtoblock%}
{% addtoblock "js" %}[my site's library js.] {% endaddtoblock %}

{% render_block "js" %}
</body>
</html>

Now in the template loaded for my DjangoCMS custom plugin,

great_calendar_plugin_template.html

{% load sekizai_tags and_a_couple_other_things %}
{% addtoblock "js" %}[plugin javascript file dependency]{%endaddtoblock %}
{% addtoblock "js" %}[plugin javascript file]{% endaddtoblock %}
....

So no matter what I do the plugin javascript files are placed into the final HTML above JQuery and all the other dependencies, rather than underneath where they belong. What am I missing here?

Thanks.

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
Zak Patterson
  • 389
  • 5
  • 11

1 Answers1

5

You can fix this issue by putting the "base" addtoblock calls (jquery, etc) as far at the top of your base template as possible. The important bit is that it's before you call and {% placeholder %} tags, which on most sites means before your opening <body> tag.

An example dummy template:

{% load cms_tags sekizai_tags and_a_bunch_of_other_stuff %}

{% addtoblock "js" %}[jquery]{% endaddtoblock %}
{% addtoblock "js" %}[google api, data, more cool stuff like jqplot.]{%endaddtoblock%}
{% addtoblock "js" %}[my site's library js.] {% endaddtoblock %}

<html>
    <head>
        {% render_block "css" %}
    </head>
    <body>
        {% placeholder "mycontent" %}

        {% render_block "js" %}
    </body>
</html>
ojii
  • 4,729
  • 2
  • 23
  • 34
  • I'm not sure if this warrants another question post, but when the admin user is logged in, the adminmedia js files get added to the 'js' block, so jquery gets redefined (which blows away jquery-ui and my jquery plugins from my libs). Is there any way to ensure that the adminmedia instead gets rendered at the top of the block? – Zak Patterson Dec 26 '11 at 17:53
  • I finally determined that the best thing for me was to reinstall django-cms with a little tweak in the [toolbar.html](https://github.com/divio/django-cms/blob/develop/cms/templates/cms/toolbar/toolbar.html) file, to conditionally write in the jQuery javascript script tag depending on whether $ was defined. I read that the cms toolbar was in redevelopment, I hope there's an upgrade before I launch this thing! – Zak Patterson Dec 26 '11 at 23:02
  • 1
    This does not work for me using 1.5 and django-cms 2.4.2. I put the addtoblock as the very first entry in my base template and it still appears before all the django-cms javascript files. – Paul Whipp Jun 03 '13 at 02:04