0

Does anybody know a way to adjust the included JS/CSS resources in a template based on the apps you've installed?

Let's say we have a basic feature in app x using template.html, and this requires foo.js which is provided in the static files for the app.

What I'd like is a way of saying an additional and optional app y can register bar.js to be included in template.html as well and this provides some advanced functionality.

Ideally, this should be tied in on a feature level - so I register both foo.js and bar.js to provide for feature A and in my template I just indicate I want all the static content for A.

shauns
  • 69
  • 5
  • 1
    are you using django-pipeline or any compression libraries? If yes, you can do it in a clean way – karthikr Aug 08 '13 at 14:07
  • a-ha - are you referring to http://django-pipeline.readthedocs.org/en/latest/configuration.html#specifying-files ? Looks like a possibility. Thanks for that - – shauns Aug 08 '13 at 15:20
  • I would any day prefer this, as it gives you compression for free. – karthikr Aug 08 '13 at 15:23

1 Answers1

0

You can follow the django admin framework approach. In your base template have an extra section for style and javascript. Based on some condition you can insert the new files.

For Example:

Define these two blocks in your base template

{% block extracss %}{% endblock %}
{% block extrajs %}{% endblock %}

If you want to add a js or css based on some condition, you can add a check inside

    {% block extracss %}
       {% if new_app_installed %}
          # Insert your CSS
       {% else %} 
          # Default 
       {% endif %}
     {% endblock %}

You can also check if your plugin app is installed and pass this context variable from view to template.

from django.conf import settings

if "new_app" in settings.INSTALLED_APPS:
    is_new_app_installed = True
Siva Arunachalam
  • 7,582
  • 15
  • 79
  • 132
  • But what would be the way to check if `new_app_installed`? I wouldn't want the 'normal' app to know about this plugged-in functionality in advance. – shauns Aug 08 '13 at 14:08
  • 1
    Yes. You can check the settings.INSTALLED_APPS. – Siva Arunachalam Aug 08 '13 at 14:11
  • Yes, but that would mean knowing the app to check for - the plug-in shouldn't be known in advance otherwise this is trivial. – shauns Aug 08 '13 at 14:33