| | 1 | = Replacing `py:extends` with Includes = |
| | 2 | |
| | 3 | As noted in MarkupVsKid, Markup does not support the `py:extends` directives for template reuse. |
| | 4 | |
| | 5 | This page shows how the [http://www.kid-templating.org/language.html#template-reuse-py-extends example] from the Kid language specification translates to a functionally equivalent setup in Markup: |
| | 6 | |
| | 7 | Assume the following base template, stored as `base.html`: |
| | 8 | |
| | 9 | {{{ |
| | 10 | #!xml |
| | 11 | <html xmlns:py="http://markup.edgewall.org/" py:strip=""> |
| | 12 | |
| | 13 | <ul py:def="display_errors(errors)"> |
| | 14 | <li py:for="error in errors" py:content="error" /> |
| | 15 | </ul> |
| | 16 | |
| | 17 | <strong py:match="b" py:content="select('text()')" /> |
| | 18 | |
| | 19 | </html> |
| | 20 | }}} |
| | 21 | |
| | 22 | And the following template “extending” that base template, stored as `page.html`: |
| | 23 | |
| | 24 | {{{ |
| | 25 | #!xml |
| | 26 | <html xmlns:py="http://markup.edgewall.org/" |
| | 27 | xmlns:xi="http://www.w3.org/2001/XInclude"> |
| | 28 | <xi:include href="base.html" /> |
| | 29 | <head> |
| | 30 | <title>Errors</title> |
| | 31 | </head> |
| | 32 | <body> |
| | 33 | <p>The following <b>errors</b> were found:</p> |
| | 34 | ${display_errors(["Field is required", "Must be phone number.."])} |
| | 35 | </body> |
| | 36 | </html> |
| | 37 | }}} |
| | 38 | |
| | 39 | The “trick” here is that the base template is included at the top of the page template. This results in the template function `display_errors()` being available to the page template, and the match template being applied to the output generated by the page template. |
| | 40 | |
| | 41 | When rendered, these will produce the following output: |
| | 42 | |
| | 43 | {{{ |
| | 44 | #!xml |
| | 45 | <html> |
| | 46 | <head> |
| | 47 | <title>Errors</title> |
| | 48 | </head> |
| | 49 | <body> |
| | 50 | <p>The following <strong>errors</strong> were found:</p> |
| | 51 | <ul> |
| | 52 | <li>Field is required</li><li>Must be phone number..</li> |
| | 53 | </ul> |
| | 54 | </body> |
| | 55 | </html> |
| | 56 | }}} |
| | 57 | |
| | 58 | ---- |
| | 59 | See also: MarkupRecipes/PyLayoutEquivalent, MarkupVsKid, MarkupTemplates |