This repository has been archived on 2024-06-20. You can view files and clone it, but cannot push or open issues or pull requests.
peryoudow/vendor/league/plates/docs/templates/layouts.md
2020-01-06 14:23:12 +01:00

2.3 KiB

layout permalink title
default templates/layouts/ Layouts

Layouts

The layout() function allows you to define a layout template that a template will implement. It's like having separate header and footer templates in one file.

Define a layout

The layout() function can be called anywhere in a template, since the layout template is actually rendered second. Typically it's placed at the top of the file.

<?php $this->layout('template') ?>

<h1>User Profile</h1>
<p>Hello, <?=$this->e($name)?></p>

This function also works with folders:

<?php $this->layout('shared::template') ?>

Assign data

To assign data (variables) to a layout template, pass them as an array to the layout() function. This data will then be available as locally scoped variables within the layout template.

<?php $this->layout('template', ['title' => 'User Profile']) ?>

Accessing the content

To access the rendered template content within the layout, use the section() function, passing 'content' as the section name. This will return all outputted content from the template that hasn't been defined in a section.

<html>
<head>
    <title><?=$this->e($title)?></title>
</head>
<body>

<?=$this->section('content')?>

</body>
</html>

Stacked layouts

Plates allows stacking of layouts, allowing even further simplification and organization of templates. Instead of just using one main layout, it's possible to break templates into more specific layouts, which themselves implement a main layout. Consider this example:

The main site layout

template.php
~~~ php <html> <head> </head> section('content')?> </html> ~~~

The blog layout

blog.php
~~~ php layout('template') ?>

The Blog

section('content')?>
~~~

A blog article

blog-article.php
~~~ php layout('blog', ['title' => $article->title]) ?>

e($article->title)?>

e($article->content)?>
~~~