Nested patterns

One of the most powerful ideas behind template systems like Patterns is the ability to include patterns within patterns, this opens many new ways of organizing and re-using your code.

Maybe the first idea that comes to mind is to put the header and the footer of your HTML document in two separate patterns because they're almost always the same in all the pages of your website. Let's do it.

Let's assume you save the header for your pages in a pattern called header.html, and it looks like this:

<!DOCTYPE html>

And your footer.html looks like this:


Then, you simply include those patterns in your main pattern. Because patterns have access to the Cherrycake engine, this is done by simply calling the Patterns::parse method you already know of, but this time inside your pattern:

<?= $e->Patterns->parse("header.html") ?>

    <p>Here's a gift for you: <?= $emoji ?></p>

<?= $e->Patterns->parse("footer.html") ?>

See this example working in the Cherrycake documentation examples site.

Last updated