ForgeViewEngine Module
Overview
ForgeViewEngine is a lightweight PHP view engine that provides essential templating features without unnecessary complexity. It supports layouts, data passing to views, and view compilation for improved performance.
Configuration
Configure the view engine through the forge_view_engine.php
configuration file:
return [
'paths' => [], // Additional view paths
'cache' => 'storage/compiled_views', // Path for compiled views
];
Configuration Options
paths
: Array of additional paths to search for viewscache
: Directory where compiled views are stored
Usage
Here's how to use the view engine in your controllers:
class HomeController
{
/**
* @inject
*/
private ViewEngineInterface $view;
public function index(Request $request): Response
{
$data = [
'title' => 'Forge Framework',
'users' => ['name' => 'Bob L', 'isLoggedIn' => true],
'links' => [
['label' => 'Documentation', 'url' => '#'],
['label' => 'GitHub', 'url' => 'https://github.com/forge-engine']
]
];
return $this->view->render('landing.index', $data, 'base');
}
}
Layouts
Create reusable layouts using the layout system:
Layout File (base.php)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Forge - The PHP Framework Where You Are In Control</title>
<link rel="stylesheet" href="/assets/css/style.css">
</head>
<body>
<div class="wrapper">
{{content}}
</div>
</body>
</html>
The {{content}}
placeholder will be replaced with the content of your view.
Data Passing
Pass data to your views and access it within the templates:
View File (index.php)
<?php
use Forge\Core\Helpers\View;
/** @var array $data */
$links = ['links' => $data['links']];
?>
<div class="landing-wrapper">
<div class="landing-container">
<h1>
<p class="forge-logo"><?php echo $data['title']; ?></p>
</h1>
<?php View::component('forge-links-list', $links); ?>
</div>
</div>
Features
- Access data using the
$data
array in views - Use components with
View::component()
- Support for nested layouts
- Clean separation of logic and presentation
Note: View compilation is currently a work in progress feature that will provide performance improvements in future updates.