Skip to content

Commit a032987

Browse files
committed
adding additional django template examples
1 parent 270ea64 commit a032987

15 files changed

+1522
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
title: django.template.base Context Example Code
2+
category: page
3+
slug: django-template-base-context-examples
4+
sortorder: 500011363
5+
toc: False
6+
sidebartitle: django.template.base Context
7+
meta: Python example code for the Context class from the django.template.base module of the Django project.
8+
9+
10+
Context is a class within the django.template.base module of the Django project.
11+
12+
13+
## Example 1 from django-pipeline
14+
[django-pipeline](https://github.com/jazzband/django-pipeline)
15+
([project documentation](https://django-pipeline.readthedocs.io/en/latest/)
16+
and
17+
[PyPI package information](https://pypi.org/project/django-pipeline/))
18+
is a code library for handling and compressing
19+
[static content assets](/static-content.html) when handling requests in
20+
[Django](/django.html) web applications.
21+
22+
The django-pipeline project is open sourced under the
23+
[MIT License](https://github.com/jazzband/django-pipeline/blob/master/LICENSE.txt)
24+
and it is maintained by the developer community group
25+
[Jazzband](https://jazzband.co/).
26+
27+
[**django-pipeline / pipeline / templatetags / pipeline.py**](https://github.com/jazzband/django-pipeline/blob/master/pipeline/templatetags/pipeline.py)
28+
29+
```python
30+
# pipeline.py
31+
import logging
32+
import subprocess
33+
34+
from django.contrib.staticfiles.storage import staticfiles_storage
35+
36+
from django import template
37+
~~from django.template.base import Context, VariableDoesNotExist
38+
from django.template.loader import render_to_string
39+
from django.utils.safestring import mark_safe
40+
41+
from ..collector import default_collector
42+
from ..conf import settings
43+
from ..exceptions import CompilerError
44+
from ..packager import Packager, PackageNotFound
45+
from ..utils import guess_type
46+
47+
logger = logging.getLogger(__name__)
48+
49+
register = template.Library()
50+
51+
52+
class PipelineMixin(object):
53+
request = None
54+
_request_var = None
55+
56+
@property
57+
def request_var(self):
58+
if not self._request_var:
59+
self._request_var = template.Variable('request')
60+
return self._request_var
61+
62+
63+
64+
## ... source file continues with no further Context examples...
65+
66+
```
67+
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
title: django.template.base FilterExpression Example Code
2+
category: page
3+
slug: django-template-base-filterexpression-examples
4+
sortorder: 500011364
5+
toc: False
6+
sidebartitle: django.template.base FilterExpression
7+
meta: Python example code for the FilterExpression class from the django.template.base module of the Django project.
8+
9+
10+
FilterExpression is a class within the django.template.base module of the Django project.
11+
12+
13+
## Example 1 from django-sitetree
14+
[django-sitetree](https://github.com/idlesign/django-sitetree)
15+
([project documentation](https://django-sitetree.readthedocs.io/en/latest/)
16+
and
17+
[PyPI package information](https://pypi.org/project/django-sitetree/))
18+
is a [Django](/django.html) extension that makes it easier for
19+
developers to add site trees, menus and breadcrumb navigation elements
20+
to their web applications.
21+
22+
The django-sitetree project is provided as open source under the
23+
[BSD 3-Clause "New" or "Revised" License](https://github.com/idlesign/django-sitetree/blob/master/LICENSE).
24+
25+
[**django-sitetree / sitetree / templatetags / sitetree.py**](https://github.com/idlesign/django-sitetree/blob/master/sitetree/templatetags/sitetree.py)
26+
27+
```python
28+
# sitetree.py
29+
from django import template
30+
~~from django.template.base import FilterExpression
31+
from django.template.loader import get_template
32+
33+
from ..sitetreeapp import get_sitetree
34+
35+
register = template.Library()
36+
37+
38+
@register.tag
39+
def sitetree_tree(parser, token):
40+
tokens = token.split_contents()
41+
use_template = detect_clause(parser, 'template', tokens)
42+
tokens_num = len(tokens)
43+
44+
if tokens_num in (3, 5):
45+
tree_alias = parser.compile_filter(tokens[2])
46+
return sitetree_treeNode(tree_alias, use_template)
47+
else:
48+
raise template.TemplateSyntaxError(
49+
'%r tag requires two arguments. E.g. {%% sitetree_tree from "mytree" %%}.' % tokens[0])
50+
51+
52+
@register.tag
53+
def sitetree_children(parser, token):
54+
tokens = token.split_contents()
55+
56+
57+
## ... source file abbreviated to get to FilterExpression examples ...
58+
59+
60+
61+
def get_value(self, context):
62+
return get_sitetree().get_current_page_attr('description', self.item, context)
63+
64+
65+
class sitetree_page_hintNode(SimpleNode):
66+
67+
def get_value(self, context):
68+
return get_sitetree().get_current_page_attr('hint', self.item, context)
69+
70+
71+
def detect_clause(parser, clause_name, tokens):
72+
if clause_name in tokens:
73+
t_index = tokens.index(clause_name)
74+
clause_value = parser.compile_filter(tokens[t_index + 1])
75+
del tokens[t_index:t_index + 2]
76+
else:
77+
clause_value = None
78+
return clause_value
79+
80+
81+
def render(context, tree_items, use_template):
82+
context.push()
83+
context['sitetree_items'] = tree_items
84+
85+
~~ if isinstance(use_template, FilterExpression):
86+
use_template = use_template.resolve(context)
87+
88+
content = get_template(use_template).render(context.flatten())
89+
context.pop()
90+
91+
return content
92+
93+
94+
95+
## ... source file continues with no further FilterExpression examples...
96+
97+
```
98+
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
title: django.template.base Node Example Code
2+
category: page
3+
slug: django-template-base-node-examples
4+
sortorder: 500011365
5+
toc: False
6+
sidebartitle: django.template.base Node
7+
meta: Python example code for the Node class from the django.template.base module of the Django project.
8+
9+
10+
Node is a class within the django.template.base module of the Django project.
11+
12+
13+
## Example 1 from django-angular
14+
[django-angular](https://github.com/jrief/django-angular)
15+
([project examples website](https://django-angular.awesto.com/classic_form/))
16+
is a library with helper code to make it easier to use
17+
[Angular](/angular.html) as the front-end to [Django](/django.html) projects.
18+
The code for django-angular is
19+
[open source under the MIT license](https://github.com/jrief/django-angular/blob/master/LICENSE.txt).
20+
21+
[**django-angular / djng / templatetags / djng_tags.py**](https://github.com/jrief/django-angular/blob/master/djng/templatetags/djng_tags.py)
22+
23+
```python
24+
# djng_tags.py
25+
import json
26+
27+
from django.template import Library
28+
~~from django.template.base import Node, NodeList, TextNode, VariableNode
29+
from django.utils.html import format_html
30+
from django.utils.safestring import mark_safe
31+
from django.utils.translation import get_language_from_request
32+
33+
from djng.core.urlresolvers import get_all_remote_methods, get_current_remote_methods
34+
35+
36+
register = Library()
37+
38+
39+
@register.simple_tag(name='djng_all_rmi')
40+
def djng_all_rmi():
41+
return mark_safe(json.dumps(get_all_remote_methods()))
42+
43+
44+
@register.simple_tag(name='djng_current_rmi', takes_context=True)
45+
def djng_current_rmi(context):
46+
return mark_safe(json.dumps(get_current_remote_methods(context.get('view'))))
47+
48+
49+
@register.simple_tag(name='load_djng_urls', takes_context=True)
50+
def djng_urls(context, *namespaces):
51+
raise DeprecationWarning(
52+
"load_djng_urls templatetag is deprecated and has been removed from this version of django-angular."
53+
"Please refer to documentation for updated way to manage django urls in angular.")
54+
55+
56+
~~class AngularJsNode(Node):
57+
def __init__(self, django_nodelist, angular_nodelist, variable):
58+
self.django_nodelist = django_nodelist
59+
self.angular_nodelist = angular_nodelist
60+
self.variable = variable
61+
62+
def render(self, context):
63+
if self.variable.resolve(context):
64+
return self.angular_nodelist.render(context)
65+
return self.django_nodelist.render(context)
66+
67+
68+
@register.tag
69+
def angularjs(parser, token):
70+
bits = token.contents.split()
71+
if len(bits) < 2:
72+
bits.append('1')
73+
values = [parser.compile_filter(bit) for bit in bits[1:]]
74+
django_nodelist = parser.parse(('endangularjs',))
75+
angular_nodelist = NodeList()
76+
for node in django_nodelist:
77+
if isinstance(node, VariableNode):
78+
tokens = node.filter_expression.token.split('.')
79+
token = tokens[0]
80+
for part in tokens[1:]:
81+
82+
83+
## ... source file continues with no further Node examples...
84+
85+
```
86+
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
title: django.template.base NodeList Example Code
2+
category: page
3+
slug: django-template-base-nodelist-examples
4+
sortorder: 500011366
5+
toc: False
6+
sidebartitle: django.template.base NodeList
7+
meta: Python example code for the NodeList class from the django.template.base module of the Django project.
8+
9+
10+
NodeList is a class within the django.template.base module of the Django project.
11+
12+
13+
## Example 1 from django-angular
14+
[django-angular](https://github.com/jrief/django-angular)
15+
([project examples website](https://django-angular.awesto.com/classic_form/))
16+
is a library with helper code to make it easier to use
17+
[Angular](/angular.html) as the front-end to [Django](/django.html) projects.
18+
The code for django-angular is
19+
[open source under the MIT license](https://github.com/jrief/django-angular/blob/master/LICENSE.txt).
20+
21+
[**django-angular / djng / templatetags / djng_tags.py**](https://github.com/jrief/django-angular/blob/master/djng/templatetags/djng_tags.py)
22+
23+
```python
24+
# djng_tags.py
25+
import json
26+
27+
from django.template import Library
28+
~~from django.template.base import Node, NodeList, TextNode, VariableNode
29+
from django.utils.html import format_html
30+
from django.utils.safestring import mark_safe
31+
from django.utils.translation import get_language_from_request
32+
33+
from djng.core.urlresolvers import get_all_remote_methods, get_current_remote_methods
34+
35+
36+
register = Library()
37+
38+
39+
@register.simple_tag(name='djng_all_rmi')
40+
def djng_all_rmi():
41+
return mark_safe(json.dumps(get_all_remote_methods()))
42+
43+
44+
@register.simple_tag(name='djng_current_rmi', takes_context=True)
45+
def djng_current_rmi(context):
46+
return mark_safe(json.dumps(get_current_remote_methods(context.get('view'))))
47+
48+
49+
@register.simple_tag(name='load_djng_urls', takes_context=True)
50+
def djng_urls(context, *namespaces):
51+
raise DeprecationWarning(
52+
"load_djng_urls templatetag is deprecated and has been removed from this version of django-angular."
53+
"Please refer to documentation for updated way to manage django urls in angular.")
54+
55+
56+
class AngularJsNode(Node):
57+
def __init__(self, django_nodelist, angular_nodelist, variable):
58+
self.django_nodelist = django_nodelist
59+
self.angular_nodelist = angular_nodelist
60+
self.variable = variable
61+
62+
def render(self, context):
63+
if self.variable.resolve(context):
64+
return self.angular_nodelist.render(context)
65+
return self.django_nodelist.render(context)
66+
67+
68+
@register.tag
69+
def angularjs(parser, token):
70+
bits = token.contents.split()
71+
if len(bits) < 2:
72+
bits.append('1')
73+
values = [parser.compile_filter(bit) for bit in bits[1:]]
74+
django_nodelist = parser.parse(('endangularjs',))
75+
~~ angular_nodelist = NodeList()
76+
for node in django_nodelist:
77+
if isinstance(node, VariableNode):
78+
tokens = node.filter_expression.token.split('.')
79+
token = tokens[0]
80+
for part in tokens[1:]:
81+
if part.isdigit():
82+
token += '[%s]' % part
83+
else:
84+
token += '.%s' % part
85+
node = TextNode('{{ %s }}' % token)
86+
angular_nodelist.append(node)
87+
parser.delete_first_token()
88+
return AngularJsNode(django_nodelist, angular_nodelist, values[0])
89+
90+
91+
@register.simple_tag(name='djng_locale_script', takes_context=True)
92+
def djng_locale_script(context, default_language='en'):
93+
language = get_language_from_request(context['request'])
94+
if not language:
95+
language = default_language
96+
return format_html('angular-locale_{}.js', language.lower())
97+
98+
99+
100+
## ... source file continues with no further NodeList examples...
101+
102+
```
103+

0 commit comments

Comments
 (0)