Gelembjuk / templating

Unified PHP templater interface. Now supports Smarty and Twig. Allows to change a template engine without PHP code changes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Gelembjuk/Templating

Common interface for different PHP templating engines like Smarty, Twig. This package allows to switch between engines witout changes in your PHP code.

The package was created to be able to migrate to different popular PHP templating engines without need to change my application code. However, it is still needed to modify templates.

Installation

Using composer: gelembjuk/templating require: {"gelembjuk/templating": "1.*"}

Additionally, you have to install a template engine you want to use - smarty/smarty or twig/twig (later i hope to add support of other engines).

Configuration

Configuration is done in run time with a constructor options (as hash argument)

$templateroptions = array(
		'templatepath' => $templatesdir, // directory where templates are stored
		'compiledir' => $compiledir,    // directory to use for compilation temp files. It is needed fro Smarty, but not Twig 
		'usecache' => false,       // don't cache . If true then use cache
		'cachedir' => $cachedir,   // path to cache directory. used if `usecache` is true
		'extension' => 'htm' // our templates files will have htm extension
	);

Usage

require '../vendor/autoload.php';

$templater = new Gelembjuk\Templating\SmartyTemplating();
// $templater = new Gelembjuk\Templating\TwigTemplating();

// init template engine
$templater->init($templateroptions);

$templater->setVar('firstname',"John");
$templater->setVar('lastname',"Smith");

$templater->setTemplate('homepage');

// fetch page page content
$pagecontent = $templater->fetchTemplate();

echo $pagecontent;

File homepage.htm in case of Smarty

<div>

<h1>Hello {$lastname}, {$firstname} </h1> 

</div>

File homepage.htm in case of Twig

<div>

<h1>Hello {{ lastname }}, {{ firstname }} </h1> 

</div>

Author

Roman Gelembjuk (@gelembjuk)

About

Unified PHP templater interface. Now supports Smarty and Twig. Allows to change a template engine without PHP code changes

License:MIT License


Languages

Language:PHP 95.0%Language:HTML 5.0%