Skip to content

Commit cb44331

Browse files
authored
Add some documentation improvements (#1)
1 parent 9258eaa commit cb44331

File tree

1 file changed

+101
-94
lines changed

1 file changed

+101
-94
lines changed

docs/src/pages/manual.md

Lines changed: 101 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,95 @@ These operators help manipulate data, control flow, and structure expressions dy
112112
- `[]`: Access list elements and dictionary keys.
113113
- `.`: Access object attributes (`user.name`).
114114

115+
!!! note
116+
Note that in Jinja2 syntax, element indexing starts from `0`.
117+
115118
For more information see the [source](https://jinja.palletsprojects.com/en/stable/templates/#math).
116119

120+
## Whitespace control
121+
122+
Jinja2 provides whitespace control to manage extra spaces and newlines in rendered templates.
123+
Useful for keeping clean template output, avoiding extra newlines in generated text, and improving readability in formatted templates.
124+
125+
#### How to Control Whitespace:
126+
- Use `-` inside `{% ... %}` or `{{ ... }}` to trim surrounding spaces and newlines.
127+
- For example: `{%- statement -%}` removes spaces before and after the statement.
128+
129+
For more information see the [source](https://jinja.palletsprojects.com/en/stable/templates/#whitespace-control).
130+
131+
#### Examples
132+
133+
```@example
134+
using Jinja2Cpp # hide
135+
tmpl = Jinja2Template("""
136+
Task:
137+
{{ 'Complete report' }}
138+
Priority:
139+
{{ 'High' }}
140+
""");
141+
142+
result = jinja2_render(tmpl);
143+
144+
println(result)
145+
```
146+
147+
---
148+
149+
```@example
150+
using Jinja2Cpp # hide
151+
tmpl = Jinja2Template("""
152+
Task:
153+
{{- 'Check mail' }}
154+
Priority:
155+
{{- 'Low' }}
156+
""");
157+
158+
result = jinja2_render(tmpl);
159+
160+
println(result)
161+
```
162+
163+
---
164+
165+
```@example
166+
using Jinja2Cpp # hide
167+
tmpl = Jinja2Template("""
168+
Task:
169+
{{- 'Visit meeting' -}}
170+
Priority:
171+
{{- 'Medium' -}}
172+
""");
173+
174+
result = jinja2_render(tmpl);
175+
176+
println(result)
177+
```
178+
179+
---
180+
181+
```@example
182+
using Jinja2Cpp # hide
183+
tmpl = Jinja2Template("""
184+
<!-- No whitespace control -->
185+
<ul>
186+
{% for item in ['apple', 'banana', 'cherry'] %}
187+
<li>{{ item }}</li>
188+
{% endfor %}
189+
</ul>
190+
191+
<!-- With whitespace control -->
192+
<ul>
193+
{% for item in ['apple', 'banana', 'cherry'] -%}
194+
<li>{{ item }}</li>
195+
{% endfor -%}
196+
</ul>
197+
""");
198+
199+
result = jinja2_render(tmpl);
200+
201+
println(result)
202+
```
203+
117204
## Variables
118205

119206
The `{{ ... }}` syntax is used for outputting variables inside templates.
@@ -271,91 +358,6 @@ println(result)
271358

272359
Look for more filters at the [source](https://jinja.palletsprojects.com/en/stable/templates/#list-of-builtin-filters).
273360

274-
## Whitespace control
275-
276-
Jinja2 provides whitespace control to manage extra spaces and newlines in rendered templates.
277-
Useful for keeping clean template output, avoiding extra newlines in generated text, and improving readability in formatted templates.
278-
279-
#### How to Control Whitespace:
280-
- Use `-` inside `{% ... %}` or `{{ ... }}` to trim surrounding spaces and newlines.
281-
- For example: `{%- statement -%}` removes spaces before and after the statement.
282-
283-
For more information see the [source](https://jinja.palletsprojects.com/en/stable/templates/#whitespace-control).
284-
285-
#### Examples
286-
287-
```@example
288-
using Jinja2Cpp # hide
289-
tmpl = Jinja2Template("""
290-
Task:
291-
{{ task }}
292-
293-
Priority:
294-
{{ priority }}
295-
""");
296-
297-
result = jinja2_render(tmpl, Dict("task" => "Complete report", "priority" => "High"));
298-
299-
println(result)
300-
```
301-
302-
---
303-
304-
```@example
305-
using Jinja2Cpp # hide
306-
tmpl = Jinja2Template("""
307-
Task:
308-
{{- task }}
309-
Priority:
310-
{{- priority }}
311-
""");
312-
313-
result = jinja2_render(tmpl, Dict("task" => "Check mail", "priority" => "Low"));
314-
315-
println(result)
316-
```
317-
318-
---
319-
320-
```@example
321-
using Jinja2Cpp # hide
322-
tmpl = Jinja2Template("""
323-
Task:
324-
{{- task -}}
325-
Priority:
326-
{{- priority -}}
327-
""");
328-
329-
result = jinja2_render(tmpl, Dict("task" => "Visit meeting", "priority" => "Medium"));
330-
331-
println(result)
332-
```
333-
334-
---
335-
336-
```@example
337-
using Jinja2Cpp # hide
338-
tmpl = Jinja2Template("""
339-
<!-- No whitespace control -->
340-
<ul>
341-
{% for item in items %}
342-
<li>{{ item }}</li>
343-
{% endfor %}
344-
</ul>
345-
346-
<!-- With whitespace control -->
347-
<ul>
348-
{% for item in items -%}
349-
<li>{{ item }}</li>
350-
{% endfor -%}
351-
</ul>
352-
""");
353-
354-
result = jinja2_render(tmpl, Dict("items" => ["apple", "banana", "cherry"]));
355-
356-
println(result)
357-
```
358-
359361
## Comments
360362

361363
Jinja2 allows adding comments in templates using `{# ... #}`.
@@ -498,7 +500,6 @@ println(result)
498500

499501
See more tests [here](https://jinja.palletsprojects.com/en/stable/templates/#builtin-tests)
500502

501-
502503
## For
503504

504505
The for loop allows iterating over sequences such as lists, tuples, dictionaries, and ranges.
@@ -598,10 +599,10 @@ tmpl = Jinja2Template("""
598599
Hello, {{ username }}!
599600
{%- endmacro -%}
600601
{{ greet_user('Charlotte') }}
601-
{{ greet_user('Henry') }}
602+
{{ greet_user(name) }}
602603
""");
603604
604-
result = jinja2_render(tmpl);
605+
result = jinja2_render(tmpl, Dict("name" => "Henry"));
605606
606607
println(result)
607608
```
@@ -611,26 +612,32 @@ println(result)
611612
```@example
612613
using Jinja2Cpp # hide
613614
tmpl = Jinja2Template("""
614-
{% macro user(username, pass) -%}
615-
<user name="{{ username }}" pass="{{ pass }}"/>
615+
{% macro user(username, password) -%}
616+
<user name="{{ username }}" pass="{{ password }}"/>
616617
{%- endmacro -%}
617618
<users>
618619
{{ user('Amelia', 'Qwerty123') }}
619-
{{ user('Ethan', 'VeryStrongPass') }}
620+
{{ user(name, pass) }}
620621
</users>
621622
""");
622623
623-
result = jinja2_render(tmpl);
624+
result = jinja2_render(tmpl, Dict("name" => "Ethan", "pass" => "VeryStrongPass"));
624625
625626
println(result)
626627
```
627628

628629
## Call
629630

630631
The `{% call %}` statement allows calling a macro that generates reusable content blocks.
632+
Inside the macro, the special function `caller()` can be used to render the block of content passed from the `{% call %}` statement.
631633
This is useful when you need to pass dynamic content or wrap elements within a reusable function-like structure.
632634

633-
For more information see the [source](https://jinja.palletsprojects.com/en/stable/templates/#call).
635+
#### Key Features:
636+
- Calls a macro with or without arguments.
637+
- Allows passing a block of content to the macro.
638+
- Uses `caller()` inside the macro to render the passed content.
639+
640+
For more information see the [Child Template](@ref) or [source](https://jinja.palletsprojects.com/en/stable/templates/#call).
634641

635642
#### Examples
636643

@@ -641,7 +648,7 @@ tmpl = Jinja2Template("""
641648
<div class="box">
642649
<h2>{{ title }}</h2>
643650
<div class="content">
644-
{% block caller %}{{ caller() }}{% endblock -%}
651+
{% block content %}{{ caller() }}{% endblock -%}
645652
</div>
646653
</div>
647654
{% endmacro -%}

0 commit comments

Comments
 (0)