How to use PHP as template engine instead of Twig in Silex

How to use PHP as template engine instead of Twig in Silex

The opinion between the developers about the usage of a templating engine or not in PHP projects is very controversial and not easy end with. Developers that doesn't like templating engines like Smarty or Twig, are free to think what they want and proceed as they want, this means, keep using the verbose PHP templates. In Silex, Twig is used as default in many skeletons due to its simplicity, however you can implement PHP as a template engine as well using the Symfony Templating Engine. We'll show you in this article how to implement it and don't fail in Silex 2.

1. Install the Symfony Templating Engine

Everyone with basic PHP knowledge could implement their own templating engine for silex with PHP, however there is a better way to do it thanks to the Symfony Template Engine. The Templating component of Symfony provides all the tools needed to build any kind of template system with plain PHP. It provides an infrastructure to load template files and optionally monitor them for changes. It also provides a concrete template engine implementation using PHP with additional tools for escaping and separating templates into blocks and layouts.

To use it in Silex, you need to install the component using composer with the following command:

composer require symfony/templating

After the installation of the component, you will be able to import the classes that you need to start rendering PHP templates instead of Twig in Silex.

2. Implementing the templating engine

As you know, you can easily assign custom content to the $app variable of silex, many components are defined in this way like twig, db etc. In the same way, we will register the rendering service that won't overwrite Twig, as you may need it as well (or maybe not, however you can keep both). In your app.php file (or the entry point where $app is available) proceed to register the component:

<?php

// ./project/src/app.php

// Include the Symfony Templating Engine required classes
use Symfony\Component\Templating\PhpEngine;
use Symfony\Component\Templating\TemplateNameParser;
use Symfony\Component\Templating\Loader\FilesystemLoader;
 
// Register the "templating" provider
$app['templating'] = function(){
    // Provide the paths where 
    $loader = new FilesystemLoader(array(
        // Assuming that you have the templates inside
        // the ./yourproject/phptemplates/ directory:
        // Note: %name% needs to be at the end of the path
        __DIR__ . '/../phptemplates/%name%'
    ));

    $templating = new PhpEngine(new TemplateNameParser(), $loader);

    return $templating;
}; 

3. Using the PHP Templating Engine

The logic with the PHP templating engine remains the same, you return the result of the render method that expects as first argument the name of the php file that you want to render and as second argument an array with the name and value of variables that you want to pass to the view:

// Register the index route
$app->get('/', function () use ($app) {

    // Use the PHP templating engine to render the PHP file
    // the path of the file would be originally:
    // ./project/phptemplates/index.html.php
    return $app['templating']->render('index.html.php', array(
        "name" => "Carlos"
    ));
})->bind('homepage');

Considering that the content of our index.html.php file is the following code:

<?php
    echo "Welcome $name!";
?>

Once you access your app at the index route, you will find "Welcome Carlos!" as response. Note that the PHP Templating Engine of Symfony offers a lot of features like the implementation of layouts, slots and automatic escaping, so be sure to read more about the component in the official Symfony website here.

Happy coding !

This could interest you

Become a more social person