Template Engines
Lithe offers flexibility by allowing you to choose from various template engines, such as pure PHP, Blade, and Twig. In addition to standard engines, you can configure others to optimize the creation and rendering of dynamic interfaces.
Setting Up Lithe for Template Rendering
To enable Lithe to render template files, you need to configure some options in your application. Follow the steps below:
Templates Directory
First, define the directory where your template files are located. You can do this with the following code:
$app->set('views', __DIR__ . '/views');
Template Engine
Next, configure the template engine that will be used by Lithe. For example, to use Blade, you should set:
$app->set('view engine', 'blade');
Package Installation
After configuring the desired template engine, install the corresponding Composer package for the chosen engine to ensure everything works correctly.
Using Template Engines
Lithe supports various template engines, such as native PHP, Blade, and Twig, offering flexibility in rendering views. To use one of these engines, follow these steps:
Define the template engine
$app->set('view engine', 'default');
Create a Blade template file
In the src/views
directory, create index.php
with the content:
<html>
<body>
<h1>Hello, <?= $name ?></h1>
</body>
</html>
Install the package via Composer
Rendering Templates
Regardless of the template engine, use the same approach to render templates:
$app->get('/', function ($req, $res) {
$res->view('index', ['name' => 'William']);
});
To learn more about Blade and Twig, consult the official documentation.
Custom Template Engines
Note: This section contains advanced topics. If you do not wish to learn about this now, feel free to skip this section.
Use the engine
method of the Lithe\App
class to configure a template engine or to create your own custom engine. This allows you to choose between available engines, such as Blade or Twig, or to develop a specific solution that meets your needs, ensuring flexibility in rendering your application’s template files.
$app->engine($name, $callback);
Where:
$name
: Name of the template engine.$callback
: Function to configure the template engine (accepts the following items as parameters: the file location, the views path, and options array).
The following code is an example of implementing a very simple template engine called lithe.view
.
// Definition of the template engine "lithe.view"
$app->engine('lithe.view', function ($file, $views, $options) {
// Read the content of the template file
$content = file_get_contents($views . '/' . $file . '.lithe.view');
// Replace variables
foreach ($options as $key => $value) {
$content = str_replace('{{ ' . $key . ' }}', $value, $content);
}
// Return the rendered content
echo $content;
});
// Configure the template engine
$app->set('view engine', 'lithe.view');
Your application will now be able to render .lithe.view
files. Create a file named index.lithe.view
in the views directory with the following content:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ title }}</title>
</head>
<body>
<h1>{{ message }}</h1>
<p>This is a .lithe.view file being rendered!</p>
</body>
</html>
Then, create the following route in your application:
$app->get('/', function ($req, $res) {
$res->view('index', [
'title' => 'My Template Engine',
'message' => 'Hello World'
]);
});
When you make a request to the homepage, the index.lithe.view
file will be rendered as HTML.