Skip to content

Commit 9e12d78

Browse files
committed
update
1 parent 27e6298 commit 9e12d78

4 files changed

Lines changed: 85 additions & 48 deletions

File tree

docs/app/tmpl.rst

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
==========
2-
Templating
3-
==========
1+
=================
2+
Guide: Templating
3+
=================
44

55
This 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+
148150
Extra 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

160162
The 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

263268
TODO: the proxy object.
264269

265270
Built-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+
285312
Render Phases
286-
-------------
313+
=============
287314

288315
Each 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+
368397
Debugging
369-
---------
398+
=========
370399

371400
Enable 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,
384411
unexpected 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.

docs/app/usage.rst

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,18 @@ Directives
4848
.. rst:directive:option:: on
4949
:type: choice
5050
51-
Determinate when the template is rendered. Defaults to ``parsing``.
52-
51+
Determinate :ref:`render-phases` of template. Defaults to ``parsing``.
5352
Available values: ``['parsing', 'parsed', 'resolving']``.
5453
55-
See :external+render:ref:`phases` for more details.
56-
5754
.. rst:directive:option:: debug
5855
:type: flag
5956
60-
Enable debug report for template rendering.
57+
Enable :ref:`debug report <debug>` for template rendering.
58+
59+
.. rst:directive:option:: extra
60+
:type: space separted list
61+
62+
List of :ref:`extra-context` to be used in the template.
6163
6264
The content of the directive should be Jinja2 Template, please refer to
6365
::doc:`/fw/templating`.
@@ -112,15 +114,18 @@ Directives
112114
Render a template immediately without defining data.
113115
This is useful when you want to render some fixed content or predefined data.
114116

115-
The options of this directive are same to :rst:dir`data.template`.
117+
.. rst:directive:option:: on
118+
.. rst:directive:option:: debug
119+
.. rst:directive:option:: extra
120+
121+
The options of this directive are same to :rst:dir:`data.template`.
116122
117123
.. example::
118124
:style: grid
119125

120126
.. data.render::
121127
122-
Sphinx has **{{ _sphinx.extensions | length }}** extensions loaded.
123-
128+
``1 + 1 = {{ 1 + 1 }}``
124129
125130
.. _ext-usage-custom-directive:
126131

docs/fw/ext.rst

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,20 @@
22
Guide: Extending
33
================
44

5-
.. _extending-extra-context:
6-
75
Extending the FDL
86
=================
97

108
You can extend the :doc:`dsl` by registering custom types, flags, and by-options
11-
through the :attr:`~sphinxnotes.render.Registry.data` attribute of
12-
:data:`sphinxnotes.render.REGISTRY`.
9+
through the :py:attr:`~sphinxnotes.render.Registry.data` attribute of
10+
:py:data:`sphinxnotes.render.REGISTRY`.
1311

1412
.. _add-custom-types:
1513

1614
Adding Custom Types
1715
-------------------
1816

19-
Use :meth:`~sphinxnotes.render.data.REGISTRY.add_type` method of
20-
:data:`sphinxnotes.render.REGISTRY` to add a new type:
17+
Use :py:meth:`~sphinxnotes.render.data.REGISTRY.add_type` method of
18+
:py:data:`sphinxnotes.render.REGISTRY` to add a new type:
2119

2220
>>> from sphinxnotes.render import REGISTRY
2321
>>>
@@ -36,8 +34,8 @@ Use :meth:`~sphinxnotes.render.data.REGISTRY.add_type` method of
3634
Adding Custom Flags
3735
-------------------
3836

39-
Use :meth:`~sphinxnotes.render.data.Registry.add_flag` method of
40-
:data:`sphinxnotes.render.REGISTRY` to add a new flag:
37+
Use :py:meth:`~sphinxnotes.render.data.Registry.add_flag` method of
38+
:py:data:`sphinxnotes.render.REGISTRY` to add a new flag:
4139

4240
>>> from sphinxnotes.render import REGISTRY
4341
>>> REGISTRY.data.add_flag('unique', default=False)
@@ -50,8 +48,8 @@ True
5048
Adding Custom By-Options
5149
------------------------
5250

53-
Use :meth:`~sphinxnotes.render.data.Registry.add_by_option` method of
54-
:data:`sphinxnotes.render.REGISTRY` to add a new by-option:
51+
Use :py:meth:`~sphinxnotes.render.data.Registry.add_by_option` method of
52+
:py:data:`sphinxnotes.render.REGISTRY` to add a new by-option:
5553

5654
>>> from sphinxnotes.render import REGISTRY
5755
>>> REGISTRY.data.add_by_option('group', str)
@@ -63,7 +61,7 @@ Use :meth:`~sphinxnotes.render.data.Registry.add_by_option` method of
6361
>>> field.index
6462
['month', 'year']
6563

66-
.. _ext-extra-contexts:
64+
.. _ext-extra-context:
6765

6866
Extending Extra Contexts
6967
========================
@@ -77,10 +75,14 @@ The decorated class must be one of the following classes:
7775
:py:class:`~sphinxnotes.render.ResolvingPhaseExtraContext`,
7876
:py:class:`~sphinxnotes.render.GlobalExtraContext`.
7977

78+
TODO
79+
8080
.. _ext-filters:
8181

8282
Extending Filters
8383
=================
8484

8585
Template filters are registered by a
8686
:py:deco:`sphinxnotes.render.filter` function decorator.
87+
88+
TODO

docs/fw/tmpl.rst

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@ be comfortable with basic Jinja2_ syntax before reading this page.
1111
Jinja Environment
1212
=================
1313

14-
Templates are rendered in a sandboxed Jinja2 environment.
15-
16-
- Undefined variables raise errors by default (``undefined=DebugUndefined``)
17-
- Extension ``jinja2.ext.loopcontrols``, ``jinja2.ext.do`` are enabled by default.
18-
- Output is plain markup text, so you can generate lists, directives, roles,
19-
and other reStructuredText constructs.
20-
2114
Built-in filters
2215
----------------
2316

0 commit comments

Comments
 (0)