Learn how to render plain PHP files using the sfTemplateEngine on Symfony 1.4

Sometimes in a Symfony 1.4 project, you will need to render custom templates with a specific content to something special, for example HTML that won't appear on any part of your website but on emails, notifications etc. Partials in this project are pretty useful and may fit someway with this need, however there's a better way to do it without relying absolutely on the partials of the frameworks.

In this article, we will explain you how to install and use the sfTemplateEngine correctly in Symfony 1.4.

1. Download templating plugin Symfony 1.4

As first step, something that the articles and tutorials of the Symfony 1.4 documentation website is from where you can obtain the source code of the templating classes. You will find examples that simply use classes like sfTemplateLoaderFilesystem or sfTemplateEngine, however they assume you already have those classes. If you are working as a newbie in this framework just like me, you will need to know that the sfTemplateEngine related classes aren't included in the project by itself. You can download the code either wit Git:

git clone https://github.com/fabpot-graveyard/templating.git

Or just download the source code of the templating from the official repository at Github here. In our case, we will download the code inside a directory named templating in the root plugins directory of Symfony 1.4:

Symfony 1.4 sfTemplateEngine Structure

In our project it will have the previous structure, however you can change it if you want but don't forget to change the path in the following step.

2. Enable templating engine

Now that we have the autoloader file that will include the classes of the Template Engine of Symfony 1.4, you can enable it in your project configuration file (yourproject/config/ProjectConfiguration.class.php) using require_once:

<?php

require_once dirname(__FILE__) . '/../symfony-1.4.20/lib/autoload/sfCoreAutoload.class.php';

// 1. In this file register the autoloader of the template engine using the following line:
// Note the path where we downloaded the plugin, yours can be different !
require_once dirname(__FILE__) . '/../plugins/templating/lib/sfTemplateAutoloader.php';

// 2. And enable the sfTemplate
sfTemplateAutoloader::register();

sfCoreAutoload::register();

class ProjectConfiguration extends sfProjectConfiguration {

    public function setup() {
        // Your original code
    }
}

The trick in this step is to include the /templating/lib/sfTemplateAutoloader.php file in the project configuration and execute the static method register from the sfTemplateAutoloader class.

3. How to use the template engine

As first step, in order to use the render engine, you will need to have a specifical path to store the templates that you want to render. In our case, we will store the templates in the original templates directory of Symfony 1.4 that can be retrievable from the sf_app_template_dir constant (project/apps/frontend/templates). Inside this directory we will create a subdirectory namely emails that will store the templates that we will render using the templating engine. As example, we will have a single template namely mail_message.php file with the following content:

<html>
    <head>
        <title>Test sfTemplateEngine</title>
    </head>
    <body>
        <h1><?php echo $message . " ". $name; ?></h1>
    </body>
</html>

To render this template using the plugin, we will do it through a simple controller in our project:

<?php

class indexActions extends sfActions {

    /**
     * Example action that will return a response to the browser.
     * 
     * @param sfWebRequest $request
     * @return Response
     */
    public function executeIndex(sfWebRequest $request) {
        // 1. Create the templates loader (provide the path to the templates as first argument)
        // note that the %name% wildcard will be replaced during the render method.
        $loader = new sfTemplateLoaderFilesystem(sfConfig::get('sf_app_template_dir').'/emails/%name%.php');
        
        // 2. Create the Template Engine with the created loader
        $templateEngine = new sfTemplateEngine($loader, array(
            'php' => new sfTemplateRendererPhp()
        ));
        
        // 3. Render some template (mail_message.php) sending the parameters in the second array
        $generatedHTML = $templateEngine->render("mail_message", array(
            'message' => 'Hello',
            'name'    => 'Carlos'
        ));
        
        // 4. In this example, we will return a response to the browser with the generated HTML
        return $this->renderText($generatedHTML);
    }
}

This action will return a view with the output "Hello Carlos" as response in the browser.

Happy coding !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors