Layouts
To create a reusable layout for your application, create a new class under /application/layouts which extends from Core\Rendering\Layout. Then add a static property $view with a string value with the name of a view. The name of the class should equal to the capitalised filename, suffixed with Layout.
<?php
namespace Application\Layouts;
class DefaultLayout extends \Core\Rendering\Layout
{
static $view = 'layout';
}
Then create the relevant view. In this view the $this variable will refer to the layout instance. Use $this->content() to retrieve the layout's contents.
<!DOCTYPE html>
<html lang="<?= language() ?>">
<head>
<title>Application</title>
</head>
<body>
<main>
<?= $this->content() ?>
</main>
</body>
</html>
You can then use the layout in your controllers by calling the layout() function. Pass the name of your layout as the first parameter. Omitting the layout name will assume the layout is named default.
public function index()
{
echo layout('default');
}
Adding content
Display content in your layout by calling the content() method on the returned layout. This method accepts the same parameters as the view() function.
public function index()
{
echo layout()->content('home', [
'foo' => 'bar'
]);
}
Passing data to the layout
Pass an associative array as the second parameter of the layout function (or first parameter if you intend to use the default layout) to pass data to the layout view.
public function index()
{
echo layout([
'title' => 'Blog'
])->content('blog/index');
}
<!DOCTYPE html>
<html lang="<?= language() ?>">
<head>
<title><?= empty($title) ? '' : $title . ' - ' ?>My Application</title>
</head>
<body>
<main>
<?= $this->content() ?>
</main>
</body>
</html>