View placeholders

View placeholders are the way you inject content from view scripts – or dynamic content constructed in a Controller – into layout files.

View placeholders allow you to inject content from view scripts, or alternatively some content you've defined in a Controller file, into the layout. View scripts are probably the most common use case, though, since other content can be passed through View attributes as well. By default there's a single placeholder available, $placeholders->default, which will contain the rendered output of current page using whatever view script is selected (by default "default.php").

If you want to pass additional markup from Controller to layout, you can create a new placeholder simply by calling it: add an echo statement into the layout (<?= $placeholders->placeholder_name ?>) and then store some data to this placeholder in the Controller:

$this->view->placeholders->placeholder_name = "<p>Content for the layout.</p>";

Alternatively you can create a view script matching the placeholder name, such as /site/templates/views/scripts/home/placeholder_name.php, and wireframe will automatically render the page using that view script and inject the result into the placeholder slot:

<?php namespace ProcessWire;
echo "<p>Content for the layout.</p>";

That right there probably isn't the best possible example, so think of it this way: if you have a shared header that you use for most of your pages, you can add the markup for the shared version directly to the layout, but before outputting it check if $placeholders->header contains a value – and if it does, output it instead. This way you just need to create a header.php view file for any page you want to have a custom header.

Back to top