Skip to content

Commit 6e6574b

Browse files
committed
init push
1 parent 5b36abb commit 6e6574b

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

docs/Flask/04_templating__jinja2_integration_.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@ Jinja2 offers more than just variable substitution. You can use basic programmin
110110

111111
There are two main types of delimiters:
112112

113+
{% raw %}
113114
* `{{ ... }}`: 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.
114115
* `{% ... %}`: 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 %}
115117

116118
Let's look at some examples.
117119

@@ -158,10 +160,12 @@ def profile():
158160

159161
**Explanation:**
160162

163+
{% raw %}
161164
* `{% 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.
162165
* `{% else %}`: If the `if` condition is false, the code under `else` is used.
163166
* `{% endif %}`: Marks the end of the `if` block.
164167
* `{{ user.name }}`: Accesses the `name` attribute of the `user` dictionary passed from Python.
168+
{% endraw %}
165169

166170
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.
167171

@@ -206,10 +210,12 @@ def show_items():
206210

207211
**Explanation:**
208212

213+
{% raw %}
209214
* `{% 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`.
210215
* `<li>{{ fruit }}</li>`: Inside the loop, we display the current `fruit`.
211216
* `{% else %}`: This optional block is executed if the `items` list was empty.
212217
* `{% endfor %}`: Marks the end of the `for` loop.
218+
{% endraw %}
213219

214220
Visiting `/items` will show a bulleted list of the fruits.
215221

@@ -240,9 +246,11 @@ Just like we used `url_for` in Python ([Chapter 2: Routing System](02_routing_sy
240246

241247
**Explanation:**
242248

249+
{% raw %}
243250
* `{{ url_for('index') }}`: Generates the URL for the view function associated with the endpoint `'index'` (which is likely `/`).
244251
* `{{ url_for('show_items') }}`: Generates the URL for the `show_items` endpoint (likely `/items`).
245252
* `{{ url_for('greet_user', username='Admin') }}`: Generates the URL for the `greet_user` endpoint, filling in the `username` variable (likely `/user/Admin`).
253+
{% endraw %}
246254

247255
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.
248256

0 commit comments

Comments
 (0)