Skip to content

Commit 70ecc46

Browse files
committed
Add DOCUMENTATION.md and hopefully fix broken tests in Travis
1 parent de142b1 commit 70ecc46

3 files changed

Lines changed: 141 additions & 8 deletions

File tree

DOCUMENTATION.md

Lines changed: 136 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,147 @@
11
# Template Engine Factory Documentation
22

3+
## Table of Contents
4+
5+
1. [Updating from `1.x` to `2.x`](#updating-from-1x-to-2x)
6+
2. [Controllers](#controllers)
7+
3. [Hooks](#hooks)
8+
4. [Best Practices](#best-practices)
9+
5. [Implementing a Template Engine](#implementing-a-template-engine)
10+
6. [Running Tests](#running-tests)
11+
12+
313
## Updating from `1.x` to `2.x`
414

15+
The `2.x` major version introduces several backwards compatibility breaks. The amount of required manual update steps
16+
depends on how many features were used. The following list _should_ cover the most important changes:
17+
18+
* The API variable `$factory` is no longer available. Use `$modules->get('TemplateEngineFactory')` to get an instance of
19+
the `\ProcessWire\TemplateEngineFactory` class.
20+
* The factory has a new configuration `templates_path`, indicating where the engine's templates are stored. This config
21+
was previously defined on template engine level. If you have customized this setting, make sure to update the value in
22+
the `TemplateEngineFactory` module's config. Defaults to `templates/views`.
23+
* _Chunks_ are now called _Controllers_. While the semantics remain the same, the public API has changed. Please
24+
read the [Controllers](#controllers) chapter on how to update your code.
25+
* The method `TemplateEngineFactory::instance()` (plus some aliases) do no longer exist. Use
26+
`TemplateEngineFactory::render()` to render a given template of the engine.
27+
* On template engine level, the confusing `global_template` setting does no longer exist. Use the introduced hook
28+
`___resolveTemplate` to resolve a custom template when a page gets rendered. The default strategy still looks for a template
29+
with the same name as the ProcessWire template.
30+
31+
## Controllers
32+
33+
A _controller_ wraps a ProcessWire template executing some logic and a template file of the active engine, rendering the
34+
output. This is how the _Automatic page rendering_ feature works, by using the template file of a page as controller. By
35+
defining custom controllers, you get the the same functionality:
36+
37+
```php
38+
$factory = $modules->get('TemplateEngineFactory');
39+
40+
// Get the sidebar controller, responsible to render a sidebar.
41+
$controller = $factory->controller('controllers/sidebar.php', 'partials/sidebar.html.twig');
42+
43+
// Optional: You might pass some data to the controller.
44+
$controller->nPosts = 3;
45+
46+
// Executing the controller renders the associated template via template engine.
47+
$controller->execute();
48+
```
49+
50+
The controller and template file from the above example might look like this:
51+
52+
```php
53+
// site/templates/controllers/sidebar.php
54+
55+
$posts = $pages->find("template=blog-post,limit=$nPosts");
56+
57+
// We have access to the $view API variable, connecting us to the template.
58+
$view->posts = $posts;
59+
```
60+
61+
```twig
62+
// site/templates/views/partials/sidebar.twig.html
63+
64+
{% for post in posts %}
65+
<h3>{{ post.title }}</h3>
66+
{% endfor %}
67+
```
68+
69+
### Updating from `1.x`
70+
71+
* Use the `TemplateEngineFactory::controller` factory method instead of `TemplateEngineFactory::chunk`.
72+
* Always provide the controller file and the template file.
73+
* Call `Controller::execute` to process the logic in the controller and to render the template.
74+
75+
```php
76+
// Version 1.x
77+
$chunk = $factory->chunk('chunks/nav-item');
78+
$chunk->render();
79+
80+
// Version 2.x
81+
$controller = $factory->controller('chunks/nav-item', 'chunks/nav-item');
82+
$controller->execute();
83+
```
84+
585
## Hooks
686

7-
### `___shouldRenderPage`
87+
The TemplateEngineFactory provides several hooks to influence the automatic page rendering process.
88+
889
### `___resolveTemplate`
9-
### `___getTemplateVariables`
1090

11-
## Best Practises
91+
Use this hook to customize the template being used when rendering a page. By default, the module looks for
92+
a template with the same name as the page template's name. For example, when rendering the homepage
93+
with the `home` template, the factory loads the `home.html.twig` twig template.
94+
95+
```php
96+
wire()->addHookAfter('TemplateEngineFactory::resolveTemplate', function (HookEvent $event) {
97+
$event->return = 'customTemplate';
98+
});
99+
```
100+
101+
---
102+
103+
### `___resolveTemplateVariables`
104+
105+
This hook allows to customize the template variables available in your template. Normally,
106+
the variables are set in the controllers via `$view` API variable. For example, calling
107+
`$view->foo = 'bar'` in your controller makes the `foo` variable accessible in the template. If you
108+
need some _global_ variables available for all pages being rendered, hook them up like this:
109+
110+
```php
111+
wire()->addHookAfter('TemplateEngineFactory::resolveTemplateVariables', function (HookEvent $event) {
112+
$event->return = array_merge(
113+
$event->return,
114+
[
115+
'user' => $this->wire('user'),
116+
]
117+
);
118+
});
119+
```
120+
121+
> The above example makes the current user available in all templates. However, changes are high that the
122+
template engine already has access to all ProcessWire API variables. If you are using Twig, you can toggle this
123+
behaviour in the _TemplateEngineTwig_ module's configuration.
124+
125+
---
126+
127+
### `___shouldRenderPage`
128+
129+
Use this hook to customize if a page should be rendered by the template engine.
130+
131+
```php
132+
wire()->addHookAfter('TemplateEngineFactory::shouldRenderPage', function (HookEvent $event) {
133+
$page = $event->arguments('page');
134+
135+
// Never render pages with the api template.
136+
if ($page->template->name === 'api') {
137+
$event->return = false;
138+
}
139+
});
140+
```
141+
142+
## Best Practices
12143

13144
## Implementing a Template Engine
14145

15-
## Executing Tests
146+
## Running Tests
147+

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
[![Build Status](https://travis-ci.org/wanze/TemplateEngineFactory.svg?branch=next)](https://travis-ci.org/wanze/TemplateEngineFactory)
44
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
55

6-
A ProcessWire module integrating template engines such as Twig. It allows to render pages or individual templates
6+
A ProcessWire module integrating template engines such as _Twig_. It allows to render pages or individual templates
77
via template engine and encourages to separate logic from markup by implementing a simple _MVC_ pattern.
88

99
* For a quick introduction, please read the [Getting Started](#getting-started) section of this readme.
1010
* More information is available in the official [Documentation](DOCUMENTATION.md).
1111

1212
> Version `2.x` of this module differs from the `1.x` version in many ways. Modules providing template engines _must_ be
1313
installed with Composer. Twig is currently the only template engine implemented for the `2.x` major version. Please
14-
take a look at the [update guide](), as the new version introduces backwards compatibility breaks.
14+
take a look at the [update guide](DOCUMENTATION.md#updating-from-1x-to-2x), as the new version introduces backwards
15+
compatibility breaks.
1516

1617
## Requirements
1718

@@ -41,7 +42,7 @@ composer require wanze/template-engine-twig:^2.0
4142
```
4243

4344
This will install the _TemplateEngineTwig_ and _TemplateEngineFactory_ modules in one step, no need to install both
44-
separately. It also installs the Twig dependencies for us, pretty neat!
45+
separately. It also installs the Twig dependencies for us, pretty neat! ✌️
4546

4647
> ℹ️ This module includes test dependencies. If you are installing it on production with `composer install`, make sure to
4748
pass the `--no-dev` flag to omit autoloading any unnecessary test dependencies!.

TemplateEngineProcesswire.module.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static function getModuleInfo()
2929
];
3030
}
3131

32-
public function ready()
32+
public function init()
3333
{
3434
/** @var \ProcessWire\TemplateEngineFactory $factory */
3535
$factory = $this->wire('modules')->get('TemplateEngineFactory');

0 commit comments

Comments
 (0)