You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/pages/django-to-jinja.md
+16-21Lines changed: 16 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,7 @@ This is to make Ontospy footprint more lightweight, as well as to simplify futur
6
6
7
7
This page contains information of Django specific template filters, or constructs, that are not directly usable with Jinja and how they have been updated.
8
8
9
+
{% raw %}
9
10
10
11
## NOW
11
12
@@ -14,9 +15,7 @@ Django's template tag `now` can be used in Jinja after installing the extension
14
15
Then you can do
15
16
16
17
```python
17
-
{% raw %}
18
18
{% now 'utc', '%a, %d%b %Y %H:%M:%S'%}
19
-
{% endraw %}
20
19
```
21
20
22
21
@@ -27,21 +26,18 @@ Django's template tag `ifchanged` does not exist in Jinja. So a custom logic for
27
26
From
28
27
29
28
```python
30
-
{% raw %}
31
29
{%for each in o.annotations %}
32
30
{% ifchanged each.1 %}
33
31
{%ifnot forloop.first %}</dl>{% endif %}
34
32
<dt>{{each.1}}</dt>
35
33
{% endifchanged %}
36
34
<dd>{{each.2|linebreaks}}</dd>
37
35
{% endfor %}
38
-
{% endraw %}
39
36
```
40
37
41
38
To
42
39
43
40
```python
44
-
{% raw %}
45
41
{%for each in o.annotations() %}
46
42
{%if each.1 != variable_watcher %}
47
43
{%ifnot loop.first %}</dl>{% endif %}
@@ -51,7 +47,6 @@ To
51
47
{% endif %}
52
48
<dd>{{each.2|linebreaks}}</dd>
53
49
{% endfor %}
54
-
{% endraw %}
55
50
```
56
51
57
52
@@ -60,7 +55,7 @@ To
60
55
Django's template tag `ifchanged` has a slightly diffent syntax in [Jinja](https://jinja.palletsprojects.com/en/3.1.x/templates/#jinja-filters.default).
61
56
62
57
From
63
-
{% raw %}
58
+
64
59
```python
65
60
{{s.qname|default:each.qname}}
66
61
```
@@ -69,13 +64,13 @@ To
69
64
```python
70
65
{{s.qname|default(each.qname)}}
71
66
```
72
-
{% endraw %}
67
+
73
68
74
69
## LINEBREAKS
75
70
76
71
Django's template filter `linebreaks` does not exist in Jinja. It can be implemented as a custom filter (see this [thread on SO](https://stackoverflow.com/questions/4901483/how-to-apply-django-jinja2-template-filters-escape-and-linebreaks-correctly))
Django's templating language is 'relaxed' when it comes to resolving an object *attribute* or *method* call. In both cases, it's enough to pass the *attribute* or *method* name.
101
96
102
97
With Jinja, *method* calls need to be followed by parentheses, like in Python. See also https://stackoverflow.com/questions/59589889/difference-between-an-item-and-an-attribute-jinja-python
103
98
104
-
{% raw %}
99
+
105
100
From
106
101
```python
107
-
"""{% for each in o.annotations %}"""
102
+
{%for each in o.annotations %}
108
103
```
109
104
110
105
To
111
106
```python
112
-
"""{% for each in o.annotations() %}"""
107
+
{%for each in o.annotations() %}
113
108
```
114
-
{% endraw %}
109
+
115
110
116
111
PS this applies to `each.children()` , `each.parents()`, `each.rdf_source()` etc..
117
112
118
113
119
-
{% raw %}
114
+
120
115
## IFEQUAL
121
116
122
117
From
123
118
124
119
```python
125
-
"""{% ifequal objtype "class" #}"""
120
+
{% ifequal objtype "class"#}"""
126
121
```
127
122
128
123
To
129
124
130
125
```python
131
-
"""{% if objtype == "class" #}"""
126
+
{%if objtype =="class"#}"""
132
127
```
133
128
134
129
## WITH
135
130
136
131
From
137
132
```python
138
-
"""{% with main_entity as each #}"""
133
+
{%with main_entity as each #}"""
139
134
...
140
135
{% endwith %}
141
136
```
142
137
143
138
To
144
139
145
140
```python
146
-
"""{% set each = main_entity #}"""
141
+
{%set each = main_entity #}"""
147
142
# no need to close anything
148
143
```
149
144
@@ -154,13 +149,13 @@ See https://jinja.palletsprojects.com/en/3.1.x/templates/#comments
0 commit comments