Edgewall Software

GenshiRecipes/PyExtendsEquivalent

Version 1 (modified by cmlenz, 8 years ago)

Add recipe that shows how to translate py:extends to includes

Replacing py:extends with Includes

As noted in MarkupVsKid, Markup does not support the py:extends directives for template reuse.

This page shows how the  example from the Kid language specification translates to a functionally equivalent setup in Markup:

Assume the following base template, stored as base.html:

<html xmlns:py="http://markup.edgewall.org/" py:strip="">

  <ul py:def="display_errors(errors)">
    <li py:for="error in errors" py:content="error" />
  </ul>

  <strong py:match="b" py:content="select('text()')" />

</html>

And the following template “extending” that base template, stored as page.html:

<html xmlns:py="http://markup.edgewall.org/"
      xmlns:xi="http://www.w3.org/2001/XInclude">
  <xi:include href="base.html" />
  <head>
    <title>Errors</title>
  </head>
  <body>
    <p>The following <b>errors</b> were found:</p>
    ${display_errors(["Field is required", "Must be phone number.."])}
  </body>
</html>

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.

When rendered, these will produce the following output:

<html>
  <head>
    <title>Errors</title>
  </head>
  <body>
    <p>The following <strong>errors</strong> were found:</p>
    <ul>
      <li>Field is required</li><li>Must be phone number..</li>
    </ul>
  </body>
</html>

See also: MarkupRecipes/PyLayoutEquivalent?, MarkupVsKid, MarkupTemplates