View placeholders are probably easiest to understand as placeholders or "slots" you can define in your layout file, and then "fill" by defining a value for them from within controller classes or view files. By default there's one placeholder, $placeholders->default
, which will contain the rendered output of current page using whatever view file is selected (by default that would be "default.php").
If you need to pass markup from a Controller class to a specific location in a layout, you can do so by defining a new placeholder – which is actually as simple as adding an echo statement into the layout file (<?= $placeholders->placeholder_name ?>
) and then filling your newly created placeholder with data in the Controller class:
<?php
namespace Wireframe\Controller;
class HomeController extends \Wireframe\Controller {
public function render() {
$this->view->placeholders->placeholder_name = "<p>Content for the layout.</p>";
}
}
Alternatively you can create a view file with a name matching the placeholder name, i.e. /site/templates/views/home/placeholder_name.php, and Wireframe will automatically populate the placeholder by rendering the page using that view file:
<?php namespace ProcessWire;
echo "<p>Content for the layout.</p>";
Overriding default content with placeholder value
Since placeholders are "created" just by echoing out whatever content they might hold, you can get creative with them: for an example if you have a shared header that you use for most of your pages, you could 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 that instead:
<header>
<?php if ($placeholders->header): ?>
<?= $placeholders->header ?>
<?php else: ?>
<p>Hello world, I'm the default header!</p>
<?php endif; ?>
</header>
Now you just need a header.php view file for any page that should get a custom header.