Documentation/Templates/Standard Templates

Standard Templates

View is a component of the architectural pattern MVC (Action-Domain-Responder for the web).

Templates store the structure of the response that will be sent to the browser. Often this is HTML code containing PHP variables defined outside the template.
Templates can be nested within other templates.

Importing one template into another is accomplished in the framework through special functions.

The function view() for embedding a template from a route or controller is intended for templates with the extension .php or .twig.
When using TWIG, you won't need the standard framework functions for embedding and caching templates since TWIG provides its own tools.


#Function insertTemplate()

Code parts in included files from the /resources/views/ directory can be repetitive. To extract them into a separate template, independent of the surrounding content, use the function insertTemplate(), with the first argument specifying the template name from the /resources/views/ folder, and the second specifying an array of variables that will be available in the template by array keys. To differentiate templates from other files, it's recommended to place them in a separate /templates/ folder.

Example of how another template /resources/views/templates/counter.php is inserted into the template /resources/views/content.php, using part of the data from the first.

<?php
// File /resources/views/content.php

/**
 * @var $title string
 * @var $total int
 * @var $unique int
 */
echo "<h1>$title</h1>";

insertTemplate('templates/counter', ['totalVisitors' => $total'uniqueVisitors' => $unique]);
<?php
// File /resources/views/templates/counter.php

/**
 * @var $totalVisitors int
 * @var $uniqueVisitors int
 */
?>
<div class="metrics">
    <div>Total: <?= $totalVisitors?></div>
    <div>Unique: <?= $uniqueVisitors?></div>
</div>

#Function template()

The helper function template() is similar to insertTemplate(), but it returns the template content as a string representation, instead of outputting it at the place where it is defined.

Models Cached Templates

Page translated: chatgpt 4-o
Back to top