495
497
and you're now a template inheritance ninja!
499
Using ``<%include>`` with Template Inheritance
500
==============================================
502
A common source of confusion is the behavior of the ``<%include>`` tag,
503
often in conjunction with its interaction within template inheritance.
504
Key to understanding the ``<%include>`` tag is that it is a *dynamic*, e.g.
505
runtime, include, and not a static include. The ``<%include>`` is only processed
506
as the template renders, and not at inheritance setup time. When encountered,
507
the referenced template is run fully as an entirely separate template with no
508
linkage to any current inheritance structure.
510
If the tag were on the other hand a *static* include, this would allow source
511
within the included template to interact within the same inheritance context
512
as the calling template, but currently Mako has no static include facility.
514
In practice, this means that ``<%block>`` elements defined in an ``<%include>``
515
file will not interact with corresponding ``<%block>`` elements in the calling
518
A common mistake is along these lines:
523
<%block name="header">
528
<%include file="partials.mako">
531
<%inherit file="parent.mako">
532
<%block name="header">
536
Above, one might expect that the ``"header"`` block declared in ``child.mako``
537
might be invoked, as a result of it overriding the same block present in
538
``parent.mako`` via the include for ``partials.mako``. But this is not the case.
539
Instead, ``parent.mako`` will invoke ``partials.mako``, which then invokes
540
``"header"`` in ``partials.mako``, and then is finished rendering. Nothing
541
from ``child.mako`` will render; there is no interaction between the ``"header"``
542
block in ``child.mako`` and the ``"header"`` block in ``partials.mako``.
544
Instead, ``parent.mako`` must explicitly state the inheritance structure.
545
In order to call upon specific elements of ``partials.mako``, we will call upon
551
<%block name="header">
556
<%namespace name="partials" file="partials.mako"/>
557
<%block name="header">
562
<%inherit file="parent.mako">
563
<%block name="header">
567
Where above, ``parent.mako`` states the inheritance structure that ``child.mako``
568
is to participate within. ``partials.mako`` only defines defs/blocks that can be
569
used on a per-name basis.
571
Another scenario is below, which results in both ``"SectionA"`` blocks being rendered for the ``child.mako`` document:
577
<%block name="SectionA">
582
<%inherit file="base.mako">
583
<%include file="child.mako">
586
<%block name="SectionA">
590
The resolution is similar; instead of using ``<%include>``, we call upon the blocks
591
of ``child.mako`` using a namespace:
596
<%inherit file="base.mako">
597
<%namespace name="child" file="child.mako">
599
<%block name="SectionA">
497
604
.. _inheritance_attr:
499
606
Inheritable Attributes