Skip to content

Commit 975a7e6

Browse files
committed
Merge pull request realpython#501 from webmaven/master
Added section on Chameleon templating, for Issue realpython#382
2 parents 126d874 + 53bf93d commit 975a7e6

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

docs/scenarios/web.rst

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,65 @@ into the corresponding block in the :file:`base.html` page.
414414
</p>
415415
{% endblock %}
416416

417+
Chameleon
418+
---------
419+
`Chameleon <https://chameleon.readthedocs.org/>`_ Page Templates are an HTML/XML template
420+
engine implementation of the `Template Attribute Language (TAL) <http://en.wikipedia.org/wiki/Template_Attribute_Language>`_,
421+
`TAL Expression Syntax (TALES) <http://chameleon.readthedocs.org/en/latest/reference.html#expressions-tales>`_,
422+
and `Macro Expansion TAL (Metal) <http://chameleon.readthedocs.org/en/latest/reference.html#macros-metal>` syntaxes.
423+
424+
Chameleon is available for Python 2.5 and up (including 3.x and pypy), and
425+
is commonly used by the `Pyramid Framework <http://trypyramid.com>`_.
426+
427+
Page Templates add within your document structure special element attributes
428+
and text markup. Using a set of simple language constructs, you control the
429+
document flow, element repetition, text replacement and translation. Because
430+
of the attribute-based syntax, unrendered page templates are valid HTML and can
431+
be viewed in a browser and even edited in WYSIWYG editors. This can make
432+
round-trip collaboration with designers and prototyping with static files in a
433+
browser easier.
434+
435+
The basic TAL language is simple enough to grasp from an example:
436+
437+
.. code-block:: html
438+
439+
<html>
440+
<body>
441+
<h1>Hello, <span tal:replace="context.name">World</span>!</h1>
442+
<table>
443+
<tr tal:repeat="row 'apple', 'banana', 'pineapple'">
444+
<td tal:repeat="col 'juice', 'muffin', 'pie'">
445+
<span tal:replace="row.capitalize()" /> <span tal:replace="col" />
446+
</td>
447+
</tr>
448+
</table>
449+
</body>
450+
</html>
451+
452+
453+
The `<span tal:replace="expression" />` pattern for text insertion is common
454+
enough that if you do not require strict validity in your unrendered templates,
455+
you can replace it with a more terse and readable syntax that uses the pattern
456+
`${expression}`, as follows:
457+
458+
.. code-block:: html
459+
460+
<html>
461+
<body>
462+
<h1>Hello, ${world}!</h1>
463+
<table>
464+
<tr tal:repeat="row 'apple', 'banana', 'pineapple'">
465+
<td tal:repeat="col 'juice', 'muffin', 'pie'">
466+
${row.capitalize()} ${col}
467+
</td>
468+
</tr>
469+
</table>
470+
</body>
471+
</html>
472+
473+
474+
But keep in mind that the full `<span tal:replace="expression">Default Text</span>`
475+
syntax also allows for default content in the unrendered template.
417476

418477
.. rubric:: References
419478

0 commit comments

Comments
 (0)