Templates

The Festi Framework offers a flexible approach to working with themes and templates. This guide covers the essential aspects of setting up and customizing themes and working with templates.

Themes

A theme in the Festi Framework defines the visual appearance of your application.

For more details about themes, please refer to the Theme Documentation.

Working with Templates

Assigning Data to Templates

The Template class allows you to pass data to template files for dynamic content rendering. You can assign single variables or multiple variables at once using the assign() method.

Assigning a Single Variable

You can assign a single variable to the template using the following method:

$template = new Template('/path/to/templates/');
$template->assign('title', 'My Page Title');

Assigning Multiple Variables

To assign multiple variables, pass an associative array to the assign() method:

$template = new Template('/path/to/templates/');
$data = array(
    'title' => 'My Page Title',
    'content' => 'This is the content of the page.',
    'user' => 'John Doe'
);
$template->assign($data);

Once assigned, these variables can be accessed directly within the template file.

Rendering Templates

The fetch() method is used to render the template and return the generated output. It takes the name of the template file as its first argument, and optionally, an array of local variables and a custom template path.

Basic Template Rendering

$content = $template->fetch('example.phtml');
echo $content;

In the example.phtml file, the assigned variables will be available:

<h1><?= $title ?></h1>
<p><?= $content ?></p>

Passing Local Variables

You can also pass local variables directly to the fetch() method. These variables are available only for that particular render operation:

$localVars = array(
    'description' => 'This is a locally scoped description.'
);
$content = $template->fetch('example.phtml', $localVars);

Retrieving Data from Templates

The retrieve() method allows you to retrieve variables that have been previously assigned to the template. This is useful when you need to access or modify the data without rendering the template.

// Assign a variable
$template->assign('user', 'John Doe');

// Retrieve the value
$user = $template->retrieve('user');
echo $user;  // Outputs: John Doe

If the variable does not exist, retrieve() will return null.

Template Existence Check with isTemplateExists()

Before rendering a template, you can verify whether the template file exists using the isTemplateExists() method.

if ($template->isTemplateExists('example.phtml')) {
    echo $template->fetch('example.phtml');
} else {
    echo "Template not found!";
}

Example Workflow

Here's a typical workflow for working with templates in Festi Framework:

// Initialize the template system
$template = new Template('/path/to/templates/');

// Assign data to the template
$template->assign('title', 'Welcome to My Site');
$template->assign('user', 'John Doe');

// Retrieve a variable if needed
$userName = $template->retrieve('user');
echo "User: " . $userName;  // Outputs: User: John Doe

// Check if the template exists before rendering
if ($template->isTemplateExists('homepage.phtml')) {
    echo $template->fetch('homepage.phtml');
} else {
    echo "Template not found!";
}

Using Templates in a Plugin

You can also use templates within a plugin. In Festi Framework, plugins often use templates to generate dynamic content.

Basic Example in a Plugin

In the following example, a template is fetched inside a plugin, and the rendered content is added to the response:

<?php

class MainPlugin extends DisplayPlugin
{
    public function onDisplayMain(Response &$response)
    {
        $response->content = $this->fetch('example.phtml');

        return true;
    }
}

Passing Data into a Template from a Plugin

You can assign data to the template directly from the plugin before fetching the content. Here's how to assign both individual variables and arrays of data:

<?php

class MainPlugin extends DisplayPlugin
{
    public function onDisplayMain(Response &$response)
    {
        // Assign individual variables
        $this->userName = 'test user';
        $this->assign('userToken', '12345678');

        // Assign an array of values
        $data = array(
            'idUser' => 5
        );
        $this->assign($data);

        // Fetch and assign the template's rendered content to the response
        $response->content = $this->fetch('example.phtml');

        return true;
    }
}

In the corresponding template file (example.phtml), you can access the variables like so:

<b>User Name:</b> <?= $userName ?></br>
<b>User Token:</b> <?= $userToken ?></br>
<b>User ID:</b> <?= $idUser ?></br>