@@ -284,10 +284,11 @@ It would be surprising if, for example, a template string that uses ``{value:.2f
284284did not round the value to two decimal places when processed.
285285
286286
287- Convenience Accessors in ``Template ``
288- -------------------------------------
287+ The ``Template.values `` Property
288+ --------------------------------
289289
290- The ``Template.values `` property is equivalent to:
290+ The ``Template.values `` property is a shortcut for accessing the ``value ``
291+ attribute of each ``Interpolation `` in the template and is equivalent to:
291292
292293.. code-block :: python
293294
@@ -296,7 +297,14 @@ The ``Template.values`` property is equivalent to:
296297 return tuple (i.value for i in self .interpolations)
297298
298299
299- The ``Template.__iter__() `` method is equivalent to:
300+ Iterating ``Template `` Contents
301+ -------------------------------
302+
303+ The ``Template.__iter__() `` method provides a simple way to access the full
304+ contents of a template. It yields the string parts and interpolations in
305+ the order they appear, with empty strings omitted.
306+
307+ The ``__iter__() `` method is equivalent to:
300308
301309.. code-block :: python
302310
@@ -308,6 +316,46 @@ The ``Template.__iter__()`` method is equivalent to:
308316 yield i
309317
310318
319+ The following examples show the ``__iter__() `` method in action:
320+
321+ .. code-block :: python
322+
323+ assert list (t" " ) == []
324+
325+ assert list (t" Hello" ) == [" Hello" ]
326+
327+ name = " World"
328+ template = t" Hello {name} !"
329+ contents = list (template)
330+ assert len (contents) == 3
331+ assert contents[0 ] == " Hello "
332+ assert contents[1 ].value == " World"
333+ assert contents[1 ].expression == " name"
334+ assert contents[2 ] == " !"
335+
336+ Empty strings, which may be present in ``Template.strings ``, are not included
337+ in the output of the ``__iter__() `` method:
338+
339+ .. code-block :: python
340+
341+ first = " Eat"
342+ second = " Roquefort"
343+ template = t" {first}{second} "
344+ contents = list (template)
345+ assert len (contents) == 2
346+ assert contents[0 ].value == " Eat"
347+ assert contents[0 ].expression == " first"
348+ assert contents[1 ].value == " Roquefort"
349+ assert contents[1 ].expression == " second"
350+
351+ # However, the strings attribute contains empty strings:
352+ assert template.strings == (" " , " " , " " )
353+
354+ Template processing code can choose to work with any combination of
355+ ``strings ``, ``interpolations ``, ``values ``, and ``__iter__() `` based on
356+ requirements and convenience.
357+
358+
311359Processing Template Strings
312360---------------------------
313361
0 commit comments