| 1 | = Replacing `py:layout` with Includes = |
| 2 | |
| 3 | As noted in MarkupVsKid, Markup does not support the `py:layout` directive for template reuse. |
| 4 | |
| 5 | This page shows how the [http://www.kid-templating.org/language.html#layout-templates-py-layout example] from the Kid language specification translates to a functionally equivalent setup in Markup. |
| 6 | |
| 7 | Assume the following layout template, stored as `layout.html`: |
| 8 | |
| 9 | {{{ |
| 10 | #!xml |
| 11 | <html xmlns:py="http://markup.edgewall.org/" py:strip=""> |
| 12 | |
| 13 | <head> |
| 14 | <title>App Name - ${page_title()}</title> |
| 15 | |
| 16 | <link href="layout.css" type="text/css" rel="stylesheet" /> |
| 17 | ${page_specific_css()} |
| 18 | </head> |
| 19 | |
| 20 | <body> |
| 21 | <h1>Now viewing: ${page_title()} of App Name</h1> |
| 22 | |
| 23 | <content>Default content</content> |
| 24 | |
| 25 | <div class="footer">Page Footer Text</div> |
| 26 | </body> |
| 27 | |
| 28 | </html> |
| 29 | }}} |
| 30 | |
| 31 | And the following page-specific template, stored as `page.html`: |
| 32 | |
| 33 | {{{ |
| 34 | #!xml |
| 35 | <html xmlns:py="http://markup.edgewall.org/" |
| 36 | xmlns:xi="http://www.w3.org/2001/XInclude"> |
| 37 | |
| 38 | <py:def function="page_title">Content Page 1 of 10</py:def> |
| 39 | |
| 40 | <link py:def="page_specific_css()" |
| 41 | href="layout.css" type="text/css" rel="stylesheet" /> |
| 42 | |
| 43 | <div py:match="content"> |
| 44 | <ul> |
| 45 | <li>Content Item 1</li> |
| 46 | <li>Content Item 2</li> |
| 47 | <li>Content Item 3</li> |
| 48 | </ul> |
| 49 | </div> |
| 50 | |
| 51 | <xi:include href="layout.html" /> |
| 52 | |
| 53 | </html> |
| 54 | }}} |
| 55 | |
| 56 | The “trick” here is that the base template is included at the '''bottom''' of the page template. This results in the template functions `page_title()` and `page_specific_css()` being available to the layout template, and the match template defined in the page template being applied to the layout template. |
| 57 | |
| 58 | When rendered, the following output would be generated: |
| 59 | |
| 60 | {{{ |
| 61 | #!xml |
| 62 | <html> |
| 63 | <head> |
| 64 | <title>App Name - Content Page 1 of 10</title> |
| 65 | <link href="layout.css" type="text/css" rel="stylesheet"> |
| 66 | <link href="layout.css" type="text/css" rel="stylesheet"> |
| 67 | </head> |
| 68 | <body> |
| 69 | <h1>Now viewing: Content Page 1 of 10 of App Name</h1> |
| 70 | <div> |
| 71 | <ul> |
| 72 | <li>Content Item 1</li> |
| 73 | <li>Content Item 2</li> |
| 74 | <li>Content Item 3</li> |
| 75 | </ul> |
| 76 | </div> |
| 77 | <div class="footer">Page Footer Text</div> |
| 78 | </body> |
| 79 | </html> |
| 80 | }}} |
| 81 | |
| 82 | ---- |
| 83 | See also: MarkupRecipes/PyExtendsEquivalent, MarkupVsKid, MarkupTemplates |