PHPackages                             alemachado/template-engine-factory - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. alemachado/template-engine-factory

ActivePw-module[Templating &amp; Views](/categories/templating)

alemachado/template-engine-factory
==================================

Adds a Twig render method to Page

09PHP

Since Mar 26Pushed 11y ago1 watchersCompare

[ Source](https://github.com/alemachado/TemplateEngineFactory)[ Packagist](https://packagist.org/packages/alemachado/template-engine-factory)[ RSS](/packages/alemachado-template-engine-factory/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

TemplateEngineFactory
=====================

[](#templateenginefactory)

ProcessWire module helping to separate logic from markup. It turns ProcessWire templates into "controllers" which can interact over a new API variable with various template engines like "Smarty" or "Twig". Any template engine can be added to the factory as separate module.

\##Implemented engines

- **ProcessWire** Default engine using the class *TemplateFile* of ProcessWire. This engine ships with this module.
- **Smarty** See:
- **Twig** See:

\##Installation Install the module just like any other ProcessWire module. Check out the following guide:

\##Motivation The goal of this module is to implement the MVC pattern as simple as possible. The ProcessWire template files under /site/templates/ *can* act as controllers, containing pure logic. A controller delegates the output/markup to a corresponding template file. This delegation is abstracted by the module so that any template engine can be used by the developer.

\##Configuration

- **Template Engine** The template engine that is used to render your templates. Any installed engine is listed here. By default, you can choose "ProcessWire", the engine that ships with this module. To use another engine like "Smarty" or "Twig", download the module (see links above) and install it. Once installed, the engine is recognized and selectable here.
- **API variable** This is the variable you can use in the controllers (ProcessWire templates) to access the template of the current page.

Any specific configurations related to the engines are set in the config options of the engine itself, e.g. "TemplateEngineProcesswire". Each engine has the following default config options available:

- **Path to templates** Relative path from the site directory where template files are stored. E.g. "templates/views/" resolves to "/site/templates/views/"
- **Template files suffix** File extension of template files
- **Global template file** Filename of a template file that is used as main template behind the API variable

\##How does it work? For each controller that is outputting markup, a corresponding template file should exist (in the template files directory configured per engine). The default convention is that the template file has the same name as the controller (aka ProcessWire template):

- Template `/site/templates/views/home.php` corresponds to controller `/site/templates/home.php`
- Template `/site/templates/views/product.php` corresponds to controller `/site/templates/product.php`

Depending on the setting "Global template file" of the activated engine, the factory tries to load the template file of the current page's controller or the global template file. If a template file is found, an instance of it is accessible over the API variable. If no template file is found, the factory assumes that the controller does not output markup over the template engine. In this case, the hook to modify the behaviour of Page::render() is not attached - everything works "normal".

The following example uses the ProcessWire template engine:

```
// In controller file: /site/templates/home.php

if ($input->post->form) {
  // Do some processing, send mail, save data...
  $session->redirect('./');
}

$view->set('foo', 'bar');
$view->set('show_nav', true);
$view->set('nav_pages', $pages->get('/')->children());
```

In the example above, some logic is processed if a form was sent. Note that there is no markup generated, because this should now be done by the corresponding template file! Over the new API variable `$view`, key/value pairs are passed to the template. Here is an example how the template file could look like:

```
// In template file: /site/templates/view/home.php

Foo:
