Partials

Partials contain common markup, widgets, and any other content shared by multiple layouts and/or views.

Partials are typically small files containing similar content as your views and layouts, the only difference being that partials are intended to be reusable: whereas a view is always tied to single template, multiple views (and layouts) can easily reuse the same partial, possibly multiple times each.

Example partial

Here's an example of a partial file (/site/templates/partials/menu/breadcrumbs.php):

<?php namespace ProcessWire; ?>
<nav aria-label="<?= $aria_label ?: 'Breadcrumbs' ?>" class="breadcrumbs">
    <ul class="breadcrumbs__list">
        <?php foreach ($page->parents as $item): ?>
            <li class="breadcrumbs__list-item">
                <a class="breadcrumbs__item" href="<?= $item->url ?>">
                    <?= $item->title ?>
                </a>
            </li>
        <?php endforeach; ?>
        <li class="breadcrumbs__list-item">
            <span class="breadcrumbs__item breadcrumbs__item--active" tabindex="0" aria-label="Current page: <?= $page->title ?>">
                <?= $page->title ?>
            </span>
        </li>
    </ul>
</nav>

(Note: if you're wondering about the aria-label part, we'll get to that real soon!)

Embedding partials

A partial can be embedded into a view file using include or require:

<?php
include 'partials/menu/breadcrumbs.php';

Note that the /site/templates/ directory is automatically added to PHP's include path, so you can refer to partials with a relative path, such as in the example above. In addition Wireframe provides an alternative, object-oriented way to refer to partials:

<?php
include $partials->menu->breadcrumbs;

Embedding partials... with arguments!

Added in Wireframe 0.10.0.

Now, using a slight variation of the object-oriented embedding method ($partials variable), you can inject a set of context variables — or arguments — into a partial:

<?= $partials->menu->breadcrumbs([
    'aria_label' => 'Not your typical breadcrumbs!',
]) ?>

... and now this part will use the custom aria label we passed to the partial, instead of outputting the default value of "Breadcrumbs":

<nav aria-label="<?= $aria_label ?: 'Breadcrumbs' ?>" class="breadcrumbs">

Although partials now have the ability to take in a set of arguments, they are still (intentionally) pretty limited when it comes to more complex data processing. For more complicated needs, you should probably look into components instead.

Back to top