Rick Testore avatar Rick Testore committed 110ddad

Issue #50 Make it easier to include jQuery/jQuery UI - improved version

Comments (0)

Files changed (5)

example/core/templates/base.html

         </form>
     {% endblock %}
     </div>
-    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
-    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
+    {% load jquery_libraries %}
+    {% include_jquery_libs %}
     <!-- <script src="{{ STATIC_URL }}js/bootstrap.min.js"></script> -->
     {{ form.media.js }}
     {% block extra-js %}{% endblock %}

example/settings.py

     os.path.join(PROJECT_PATH, 'static'),
 )
 
+# Optional variables for JQuery Libraries. If not specified, defaults in templateg jquery_libraries will be used instead
+JQUERY_URL = '//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'
+#JQUERY_UI_URL = '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js'
+
 # Make this unique, and don't share it with anybody.
 SECRET_KEY = 'g2yn(u+9ue099hs@ap%n_j6nyxc5t*z8lq_avw_8ic_wnsytri'
 

example/templates/admin/base_site.html

 {% block title %}{{ title }} | Django-Selectable Example Site{% endblock title %}
 {% block extrahead %}
 <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/themes/ui-lightness/jquery-ui.css" type="text/css" />
-<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
-<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js"></script>
+    {% load jquery_libraries %}
+    {% include_jquery_libs %}
 {% endblock %}
 {% block branding %}
 <h1 id="site-name">Django-Selectable Administration</h1>
Add a comment to this file

selectable/templatetags/__init__.py

Empty file added.

selectable/templatetags/jquery_libraries.py

+# -*- encoding: utf-8 -*-
+from django import template
+from django.template import Library
+from django.conf import settings
+
+register = template.Library()
+
+@register.simple_tag
+def include_jquery_libs():
+    SCRIPT_TAG = '<script type="text/javascript" src="%s"></script>'
+
+    try:
+        jquery_url = settings.JQUERY_URL
+    except:
+        jquery_url = '//ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js'
+
+    try:
+        jquery_ui_url = settings.JQUERY_UI_URL
+    except:
+        jquery_ui_url = '//ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js'
+
+    script_libs = SCRIPT_TAG % jquery_url
+    script_libs += '\n'
+    script_libs += SCRIPT_TAG % jquery_ui_url
+    return script_libs
+
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.