Skip to content

Views & Layouts

Views Path

Views are stored in src/Views/ by default. You can change this in configuration:

php
$app->setConfig(['views_path' => __DIR__ . '/resources/views']);

Basic View

Create a view in src/Views/home.php:

php
<h1><?= $title ?></h1>
<p><?= $message ?></p>

Render from a route:

php
use Fluxor\Response;

Flow::GET()->do(function($req) {
    return Response::view('home', [
        'title' => 'Welcome',
        'message' => 'Hello World'
    ]);
});

Layouts

Create a layout in src/Views/layouts/main.php:

php
<?php use Fluxor\View; ?>
<!DOCTYPE html>
<html>
<head>
    <title><?= View::yield('title', 'Default Title') ?></title>
</head>
<body>
    <?= View::yield('content') ?>
</body>
</html>

Using Layouts

php
<?php use Fluxor\View; ?>

<?php View::extend('layouts/main'); ?>

<?php View::section('title'); ?>
    My Page Title
<?php View::endSection(); ?>

<?php View::section('content'); ?>
    <h1>Page Content</h1>
<?php View::endSection(); ?>

Sections

Define multiple named sections:

php
<?php View::section('sidebar'); ?>
    <div class="sidebar">...</div>
<?php View::endSection(); ?>

<?php View::section('content'); ?>
    <main>...</main>
<?php View::endSection(); ?>

Yield them in layout:

php
<div class="container">
    <?= View::yield('sidebar') ?>
    <?= View::yield('content') ?>
</div>

Partials

Include other views:

php
<?= View::include('components/header', ['title' => 'My Page']) ?>

Escaping

php
<?= View::e($userInput) ?>  // Escaped HTML
<?= View::raw($html) ?>      // Raw HTML (use carefully!)

Error Views

Create custom error pages in src/Views/errors/:

src/Views/errors/
├── 404.php      # Custom 404 page
├── 500.php      # Custom 500 page
└── common.php   # Generic error template (fallback)

Each error view receives $statusCode and $message variables:

php
<!DOCTYPE html>
<html>
<head>
    <title><?= $statusCode ?> - <?= $message ?></title>
</head>
<body>
    <h1><?= $statusCode ?></h1>
    <p><?= $message ?></p>
</body>
</html>

Released under the MIT License.