1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
from django import template
from django.conf import settings
register = template.Library()
@register.tag
def jquery(parser, token):
return JQueryNode()
class JQueryNode(template.Node):
def render(self, context):
# serve content from HTTPS if we know this request is secure
secure = 'secure' in context and context['secure']
prefixes = { False: 'http', True: 'https' }
version = '1.4.3'
oncdn = getattr(settings, 'CDN_ENABLED', True)
if oncdn:
jquery = '%s://ajax.googleapis.com/ajax/libs/jquery/' \
'%s/jquery.min.js' % (prefixes[secure], version)
else:
jquery = '/media/jquery-%s.min.js' % version
return '<script type="text/javascript" src="%s"></script>' % jquery
@register.tag
def cdnprefix(parser, token):
return CDNPrefixNode()
class CDNPrefixNode(template.Node):
def render(self, context):
oncdn = getattr(settings, 'CDN_ENABLED', True)
if not oncdn:
return ''
secure = 'secure' in context and context['secure']
# if left undefined, same behavior as if CDN is turned off
paths = {
False: getattr(settings, 'CDN_PATH', ''),
True: getattr(settings, 'CDN_PATH_SECURE', ''),
}
return paths[secure]
# vim: set ts=4 sw=4 et:
|