Smarty helper


1Presentation

This helper is useful for processing Smarty templates within an application, to generate feeds (text, HTML, XML or other) outside the framework's view processing.

It is instantiated via the dependency injection component.

Its render() method takes as parameters the path to the Smarty template to be used (absolute path or relative path under the project's templates/ directory), and an associative array containing the variables to be made accessible in the template.

Its eval() method is used in a similar way, but expects the first parameter to be the content of a Smarty template, rather than a path to a file.

Its templateExists() method takes as parameter the path to a template, and returns a boolean indicating whether the template exists or not.


2Usage

Simple example:

// template definition
$template = 'path/to/template.tpl';

// template data
$data = [
	'var1' => 'value1',
	'var2' => 'value2',
];

// instanciation through the loader
$html = $this->_loader['\Temma\Utils\Smarty']->render($template, $data);

Advanced example:

use \Temma\Exceptions\IO as TµIOException;

// template data
$data = [
	'name'   => 'Luke',
	'mentor' => 'Yoda',
];

try {
    // template file's path
    $path = 'chemin/vers/template.tpl';
    // traitement du template
    $html = $this->_loader['\Temma\Utils\Smarty']->render($path, $data);
} catch (TµIOException $e) {
    // variable contenant le code Smarty
    $smarty = "Bonjour {$name}, disciple de {$mentor}";
    // traitement du template
    $html = $this->_loader['\Temma\Utils\Smarty']->eval($smarty, $data);
}

// vérification de l'existence d'un fichier de template
if ($this->_loader['Temma\Utils\Smarty']->templateExists($path)) {
    print("Le template existe.");
}