1- ==========
2- Templating
3- ==========
1+ =================
2+ Guide: Templating
3+ =================
44
55This guide explains how to write Jinja2 templates for the ``sphinxnotes.render ``
66-based extension (``sphinxnotes.render.ext ``, ``sphinxnotes.any `` and etc.).
@@ -145,6 +145,8 @@ extension defines the schema through the :rst:dir:`data.schema` directive or
145145 :py:class: `~sphinxnotes.render.BaseDataDefineDirective ` or
146146 :py:class: `~sphinxnotes.render.BaseDataDefineRole ` can generate main context.
147147
148+ .. _extra-context :
149+
148150Extra Context
149151-------------
150152
@@ -158,7 +160,7 @@ so you need to declare them in advance, and load it in the template using the
158160``load_extra() `` function:
159161
160162The way of declaring extra context is vary depending on the extension you use.
161- For ``sphinxnotes.render.ext ``, :rst:dir: `data.template.extra `,
163+ For ``sphinxnotes.render.ext `` extension , :rst:dir: `data.template.extra `,
162164:rst:dir: `data.render.extra ` and the ``templat.extra `` field of
163165:confval: `data_define_directives ` are for this.
164166
@@ -170,7 +172,7 @@ For ``sphinxnotes.render.ext``, :rst:dir:`data.template.extra`,
170172
171173 {% set doc = load_extra('doc') %}
172174
173- Document Title: {{ doc.title }}
175+ Document Title: " {{ doc.title }}"
174176
175177 Built-in Extra Contexts
176178~~~~~~~~~~~~~~~~~~~~~~~
@@ -190,8 +192,8 @@ The following extra contexts are available:
190192
191193 {% set app = load_extra('sphinx') %}
192194
193- We have {{ app.extensions | length }}
194- extensions loaded.
195+ ** {{ app.extensions | length }}**
196+ extensions are loaded.
195197
196198 ``env ``
197199 :Phase: all
@@ -206,8 +208,8 @@ The following extra contexts are available:
206208
207209 {% set env = load_extra('env') %}
208210
209- We have {{ env.all_docs | length }}
210- documents.
211+ ** {{ env.all_docs | length }}**
212+ documents found .
211213
212214 ``markup ``
213215 :Phase: parsing and later
@@ -221,11 +223,14 @@ The following extra contexts are available:
221223 .. data.render::
222224 :extra: markup
223225
224- {% set markup = load_extra('markup') | jsonify %}
226+ {%
227+ set m = load_extra('markup')
228+ | jsonify
229+ %}
225230
226231 .. code::
227232
228- {% for line in markup .split('\n') -%}
233+ {% for line in m .split('\n') -%}
229234 {{ line }}
230235 {% endfor %}
231236
@@ -241,8 +246,8 @@ The following extra contexts are available:
241246 .. data.render::
242247 :extra: section
243248
244- Title of current section is
245- {{ load_extra('section').title }}
249+ Seciont Title:
250+ " {{ load_extra('section').title }}"
246251
247252 ``doc ``
248253 :Phase: parsing and later
@@ -255,15 +260,20 @@ The following extra contexts are available:
255260 .. data.render::
256261 :extra: doc
257262
258- Title of current document is
263+ Document titie:
259264 "{{ load_extra('doc').title }}".
260265
261266 .. seealso :: :ref:`extending-extra-context`
262267
263268TODO: the proxy object.
264269
265270Built-in Filters
266- ----------------
271+ ================
272+
273+ In addition to the `Builtin Jinia Filters `__, this extension also provides the
274+ following filters:
275+
276+ __ https://jinja.palletsprojects.com/en/stable/templates/#builtin-filters
267277
268278``roles ``
269279 Produces role markup from a sequence of strings.
@@ -282,8 +292,25 @@ Built-in Filters
282292 :Text: ``{{ text }}``
283293 :Rendered: {{ text }}
284294
295+ ``jsonify ``
296+ Convert value to JSON.
297+
298+ .. example ::
299+ :style: grid
300+
301+ .. data.render::
302+
303+ {% set text = {'name': 'mimi'} %}
304+
305+ :Strify: ``{{ text }}``
306+ :JSONify: ``{{ text | jsonify }}``
307+
308+ .. seealso :: :ref:`ext-filters`
309+
310+ .. _render-phases :
311+
285312Render Phases
286- -------------
313+ =============
287314
288315Each template has a render phase that determines when it is processed:
289316
@@ -365,20 +392,30 @@ Each template has a render phase that determines when it is processed:
365392 Internally, each phase corresponds to a :py:class: `~sphinxnotes.render.Phase `
366393 enum value. The ``on `` option maps to :py:attr: `~sphinxnotes.render.Template.phase `.
367394
395+ .. _debug :
396+
368397Debugging
369- ---------
398+ =========
370399
371400Enable the debug option to see a detailed report when troubleshooting templates:
372401
373- .. code-block :: rst
402+ .. example ::
403+ :style: grid
374404
375405 .. data.render::
376406 :debug:
377407
378- {{ name }}
379-
380- The debug report includes: resolved context, available extra-context keys,
381- template source, rendered markup text, and the final nodes produced.
408+ {{ 1 + 1 }}
382409
383410 This is especially useful when a template fails due to an undefined variable,
384411unexpected data shape, or invalid generated markup.
412+
413+ Jinja Template Technical Details
414+ ================================
415+
416+ Templates are rendered in a sandboxed Jinja2 environment.
417+
418+ - Undefined variables raise errors by default (``undefined=DebugUndefined ``)
419+ - Extension ``jinja2.ext.loopcontrols ``, ``jinja2.ext.do `` are enabled by default.
420+ - Output is plain markup text, so you can generate lists, directives, roles,
421+ and other reStructuredText constructs.
0 commit comments