Partials are typically small files containing similar content as your views and layouts, the main difference being that partials are intended to be reusable: whereas a view is usually tied to single template, multiple views (and layouts) can all use 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>
(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 PHP include or require expressions:
<?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, like we did in the example above.
In addition Wireframe provides an alternative, object-oriented way to refer to partials:
<?php
include $partials->menu->breadcrumbs;
As of Wireframe 0.22.0 you can also use the Partials::get() method with path-like argument:
<?php
include $partials->get('menu/breadcrumbs');
Embedding partials with arguments
Added in Wireframe 0.10.0.
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">
Partials::get() and Partials::render()
Added in Wireframe 0.22.0.
An alternative approach for embedding partials with arguments is to use the $partials->get()
method with an additional arguments array:
<?= $partials->get('menu/breadcrumbs', [
'aria_label' => 'Not your typical breadcrumbs!',
]) ?>
... or the $partials->render()
method:
<?= $partials->render('menu/breadcrumbs', [
'aria_label' => 'Not your typical breadcrumbs!',
]) ?>
Note that the behaviour of $partials->get()
depends on whether the second argument is provided. If you omit this argument, a Partial object is returned instead of a string. The $partials->render()
method, on the other hand, always returns a rendered string value.