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/Flask/04_templating__jinja2_integration_.md
+8Lines changed: 8 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -110,8 +110,10 @@ Jinja2 offers more than just variable substitution. You can use basic programmin
110
110
111
111
There are two main types of delimiters:
112
112
113
+
{% raw %}
113
114
*`{{ ... }}`: Used for **expressions**. This is where you put variables you want to display, or even simple calculations or function calls. The result is inserted into the HTML.
114
115
*`{% ... %}`: Used for **statements**. This includes things like `if`/`else` blocks, `for` loops, and other control structures. These don't directly output text but control how the template is rendered.
116
+
{% endraw %}
115
117
116
118
Let's look at some examples.
117
119
@@ -158,10 +160,12 @@ def profile():
158
160
159
161
**Explanation:**
160
162
163
+
{% raw %}
161
164
*`{% if user and user.is_logged_in %}`: Starts an `if` block. Jinja2 checks if the `user` variable exists and if its `is_logged_in` attribute is true.
162
165
*`{% else %}`: If the `if` condition is false, the code under `else` is used.
163
166
*`{% endif %}`: Marks the end of the `if` block.
164
167
*`{{ user.name }}`: Accesses the `name` attribute of the `user` dictionary passed from Python.
168
+
{% endraw %}
165
169
166
170
If you run this and visit `/profile`, you'll see the "Welcome back, Charlie!" message. If you change `current_user` to `None` in the Python code and refresh, you'll see the "Welcome, Guest!" message.
167
171
@@ -206,10 +210,12 @@ def show_items():
206
210
207
211
**Explanation:**
208
212
213
+
{% raw %}
209
214
*`{% for fruit in items %}`: Starts a `for` loop. It iterates over the `items` list passed from Python. In each iteration, the current item is assigned to the variable `fruit`.
210
215
*`<li>{{ fruit }}</li>`: Inside the loop, we display the current `fruit`.
211
216
*`{% else %}`: This optional block is executed if the `items` list was empty.
212
217
*`{% endfor %}`: Marks the end of the `for` loop.
218
+
{% endraw %}
213
219
214
220
Visiting `/items` will show a bulleted list of the fruits.
215
221
@@ -240,9 +246,11 @@ Just like we used `url_for` in Python ([Chapter 2: Routing System](02_routing_sy
240
246
241
247
**Explanation:**
242
248
249
+
{% raw %}
243
250
*`{{ url_for('index') }}`: Generates the URL for the view function associated with the endpoint `'index'` (which is likely `/`).
244
251
*`{{ url_for('show_items') }}`: Generates the URL for the `show_items` endpoint (likely `/items`).
245
252
*`{{ url_for('greet_user', username='Admin') }}`: Generates the URL for the `greet_user` endpoint, filling in the `username` variable (likely `/user/Admin`).
253
+
{% endraw %}
246
254
247
255
Using `url_for` in templates ensures that your links will always point to the correct place, even if you change the URL rules in your Python code later.
0 commit comments