Views & Templates

PHPFast makes it easy to create dynamic web pages with its flexible view system. All views are stored in the application/Views/ folder, where you can use layouts, reusable components, and pass data to structure your templates efficiently.

Views Structure

ROOT
├── application/                       # Application-specific files
│   └── Views/                         # Views for frontend and backend
│       ├── Backend/                   # Backend views
│       ├── Frontend/                  # Frontend views (assets, components)
│       │   ├── Assets/                # Frontend assets (CSS, JS)
│       │   │   ├── css/               # CSS files
│       │   │   └── js/                # JS files
│       │   ├── Component/             # Reusable components (e.g., menu, search)
│       │   │   ├── header.php         # Header component
│       │   │   ├── footer.php         # Footer component
│       │   │   └── sidebar_main.php   # Sidebar main component
│       │   └── home_index.php         # Homepage view
│       ├── backend.php                # Backend layout
│       ├── frontend.php               # Frontend layout
│       ├── dashboard.php              # Dashboard layout
│       └── 404.php                    # Error layout

Creating a View

  1. Go to the application/View/ directory inside your PHPFast project.

  2. Inside your preferred folder, create a new PHP file (e.g., frontend/home_index.php).

  3. Open the file and insert your HTML and PHP code to structure the page.

<h1>Welcome to PHPFast Framework</h1>
<p>This is the Home page.</p>

Using manual way

A layout is a reusable template (e.g., header, footer) that wraps around the main content of your pages. You can store layouts in the View/ directory for better organization and reuse.

Example frontend.php layout:

<!DOCTYPE html>
<html>
<head>
    <title><?php echo $title ?? 'PHPFast Framework'; ?></title>
    <?= !empty($assets_header) ? $assets_header : ''; ?>
</head>
    <body>
        <div>
            <?php if (isset($view)) require $view; ?>
            // $view will depend on Controller
        </div>
        <?= !empty($assets_footer) ? $assets_footer : ''; ?>
    </body>
</html>

Setup Controller

<?php

namespace App\Controllers;

use System\Core\BaseController;
use System\Core\Render;

class HomeController extends BaseController {
    public function index() {
        $data = [
            'title'   => 'Welcome to PHPFast Framework',
            'message' => 'This is the Home page.'
        ];
        
        // Set components
        $sidebar = Render::component('Frontend/Component/main_sidebar');
        
        // Setup CSS and JS 
        $this->assets->add('css', 'css/style.css', 'head');
        $this->assets->add('js', 'js/main.js', 'footer');
        
        // Send data to view
        $this->data('sidebar', $sidebar);
        $this->data('data', $data);
        
        // Send data contains CSS and JS 
        $this->data('assets_header', $this->assets->header('backend'));
        $this->data('assets_footer', $this->assets->footer('backend'));
        
        // Render view
        $this->render('frontend', 'Frontend/Home/index');
    }
}

index() return $data to view Frontend/Home/index.php and in layout frontend.php will include this view.

Get data in view

Example in Frontend/Home/index.php:

<h1><?= $message ?></h1>

Using Blocks

The PHPFast Framework organizes complex website interfaces by breaking them into independent blocks like head, header, content, and footer. This makes components easier to manage, reuse, and maintain, while also keeping interface and logic separate for better scalability.

That's why the PHPFast Framework implements a Block structure, making interface management more logical, structured, and efficient.

Find out how to use Blocks

When to use Manual or Blocks?

Depending on the first parameter of the render() method. If this layout uses Blocks, it will follow the second way.

Example:

Use the same HomeController above, but the layout display different.

Manual

If the code in layout frontend.php like this:

<!DOCTYPE html>
<html>
<head>
    <title><?php echo $title ?? 'PHPFast Framework'; ?></title>
    <?= !empty($assets_header) ? $assets_header : ''; ?>
</head>
    <body>
        <div>
            <?php if (isset($view)) require $view; ?>
            // $view will depend on Controller
        </div>
        <?= !empty($assets_footer) ? $assets_footer : ''; ?>
    </body>
</html>

Blocks

If the code in layout frontend.php like this:

<?php
    namespace System\Libraries;
?>

<?php Render::block('head'); ?>

<body>
    <div class="container-full">
        <?php Render::block('content'); ?>
    </div>
    
    <?php Render::block('footer'); ?>
</body>

Error Pages

You can create custom error pages and store them in the application/Views/ directory. For example, to handle 404 errors, create a file named 404.php in that folder.

if (!$pageFound) {
    $this->render404();
}

Last updated

Was this helpful?