Documentation MVC
Views and layouts
Plain PHP templates, page layouts, partials and variables.
Templates are PHP
olala has no separate template language: a template is an ordinary PHP file included with include.
There's nothing to compile and nothing to learn, and OPcache caches templates just like any other code.
Variables passed to render() are available directly:
<?php use olala\modules\money; ?>
<h1><?= htmlspecialchars($title) ?></h1>
<?php if ($items): ?>
<ul class="cart">
<?php foreach ($items as $item): ?>
<li><?= htmlspecialchars($item['name']) ?> — $<?= money::format($item['price']) ?></li>
<?php endforeach ?>
</ul>
<?php else: ?>
<p>Your cart is empty</p>
<?php endif ?>Escape your output. olala doesn't escape anything automatically. Output everything that came from the user or the database through htmlspecialchars() — or create a short helper such as ui::e().
Inside a template, $this is the current controller. So you can import classes with use
at the top of the file and call controller methods, including $this->render().
Partials
Repeated fragments go into separate files and are rendered from other templates:
<?php foreach ($posts as $post): ?>
<?= $this->render('card', ['post' => $post]) ?>
<?php endforeach ?><article class="card">
<h2><?= htmlspecialchars($post['title']) ?></h2>
</article>
Put templates shared by all modules (header, footer, feedback form) into the layout folder:
site/layouts/default/header.php. render('header') will find it from any module
if the module itself has no template with that name.
Layouts
A layout is the outer shell of a page. The action's result arrives in it as the $content variable:
it can be a rendered view (return $this->render(...)) or just a string (return 'Text').
The layout name is a folder name in site/layouts/; the layout file is always called index.php.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?= htmlspecialchars($title ?? 'My site') ?></title>
<link rel="stylesheet" href="/assets/css/site.css">
</head>
<body>
<?= $this->render('header') ?>
<main><?= $content ?></main>
<?= $this->render('footer') ?>
</body>
</html>Choosing a layout
The default layout is set in config.php, and any action can change it:
use olala\view;
view::$layout = 'default'; // config.php — for the whole site
// in an action:
view::$layout = 'admin'; // site/layouts/admin/index.php
view::$layout = null; // output the action's result without a layout
view::ajax(); // the 'ajax' layout — for AJAX responsesVariables in the layout
The layout receives $content and every variable passed to render() during the request.
Their full list is stored in view::$allVars. If the same variable was passed several times,
the last value wins. So check optional variables: $title ?? 'My site'.
The ajax layout
view::ajax() switches the layout to ajax. You create this layout once — it simply outputs the content:
<?= $content ?>In addition, in ajax mode the core returns errors as JSON: {"error": "…"}.
Page scripts
view::addJSScript('head' | 'foot', $url) collects script URLs in view::$jsScripts —
this way an action can ask the layout to include a file. Printing the list is up to the layout:
// in an action
view::addJSScript('foot', '/assets/js/cart.js');<?php foreach (olala\view::$jsScripts['foot'] as $src): ?>
<script src="<?= $src ?>"></script>
<?php endforeach ?>
</body>