You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+19-13Lines changed: 19 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
TemplateEngineFactory
2
2
=====================
3
-
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.
3
+
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.
4
4
5
5
##Implemented engines
6
6
***ProcessWire** Default engine using the class *TemplateFile* of ProcessWire. This engine ships with this module.
@@ -10,19 +10,25 @@ ProcessWire module helping to separate logic from markup. It turns ProcessWire t
10
10
##Installation
11
11
Install the module just like any other ProcessWire module. Check out the following guide: http://modules.processwire.com/install-uninstall/
12
12
13
+
##Motivation
14
+
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.
15
+
13
16
##Configuration
14
-
***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.
17
+
***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.
15
18
***API variable** This is the variable you can use in the controllers (ProcessWire templates) to access the template of the current page.
16
19
17
-
Any configurations related to the engines are set in the config options of the engine itself, e.g. *TemplateEngineProcesswire*.
20
+
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:
21
+
***Path to templates** Relative path from the site directory where template files are stored. E.g. "templates/views/" resolves to "/site/templates/views/"
22
+
***Template files suffix** File extension of template files
23
+
***Global template file** Filename of a template file that is used as main template behind the API variable
18
24
19
25
##How does it work?
20
-
For each controller that should output markup a corresponding template file should exist (in the directory configured per engine). The convention is that the template file *must* have the same name as the controller (aka ProcessWire template):
26
+
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):
21
27
22
28
* Template `/site/templates/views/home.php` corresponds to controller `/site/templates/home.php`
23
29
* Template `/site/templates/views/product.php` corresponds to controller `/site/templates/product.php`
24
30
25
-
The factory tries to load the template file of the current page's controller. 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() are not attached - everything works "normal".
31
+
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".
26
32
27
33
The following example uses the ProcessWire template engine:
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:
46
+
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:
41
47
```php
42
48
// In template file: /site/templates/view/home.php
43
49
@@ -52,7 +58,7 @@ In the example above, some logic is processed if a form was sent. Note that ther
52
58
</ul>
53
59
<?php endif; ?>
54
60
```
55
-
Assume there is installed the module TemplateEngineSmarty and Smarty is chosen as the active template engine. The template file could look like this:
61
+
Assume there is installed the module "TemplateEngineSmarty" and Smarty is chosen as the active template engine. The template file could look like this:
56
62
```php
57
63
// In template file: /site/templates/smarty/home.tpl
58
64
@@ -67,10 +73,10 @@ Assume there is installed the module TemplateEngineSmarty and Smarty is chosen a
67
73
</ul>
68
74
{/if}
69
75
```
70
-
The introduced API variable acts as a gateway to the active template engine. This means that the template engine can be switched at any time without the need to change the controller logic! In the previous example, the controller logic is still the same but the template engine was switched from ProcessWire to Smarty.
76
+
The introduced API variable acts as a gateway to the active template engine. This means that the template engine can be switched at any time without the need to change the controller logic. In the previous example, the controller logic is still the same but the template engine was switched from "ProcessWire" to "Smarty".
71
77
72
78
### Load and output markup of other template files
73
-
Use the TemplateEngineFactory module to load any template file and output it's markup:
79
+
Use the "TemplateEngineFactory" module to load any template file and output it's markup:
74
80
```php
75
81
// In controller file: /site/templates/product.php
The example above loads a template file called *product_chunk.tpl* and sets some variables. Calling *render()* returns the rendered markup.
90
+
The example above loads a template file called "product_chunk.tpl" and passes some variables. Calling "render()" returns the rendered markup of the template file.
85
91
86
92
## Important: Caching
87
93
Since former ProcessWire templates are now controllers that generally do not output any markup, the ProcessWire template cache should *NOT* be active. Deactivate cache in the settings of your template under the section "Cache".
88
94
89
-
It is possible for any template engine to support additional caching. At the moment only Smarty supports caching of it's own templates. In this case, the following methods are available for you (advanced usage):
95
+
It is possible for any template engine to support additional caching. At the moment only "Smarty" supports caching of it's own templates. If caching is supported by the engine, the following methods are available for you (advanced usage):
90
96
```php
91
97
// These methods are only available if the selected engine supports caching!!
92
98
@@ -104,7 +110,7 @@ $view->clearAllCache();
104
110
If caching is supported by the engine, the TemplateEngineFactory module takes care of clearing the cache whenever pages are saved or deleted.
105
111
106
112
# Implementing a template engine
107
-
Implementing another template engine is straightforward. Please take a look at the implemented engines like Smarty or Twig to see some examples. Your engine needs to extend the abstract class TemplateEngine and implement some methods.
113
+
Implementing another template engine is straightforward. Please take a look at the implemented engines like "Smarty" or "Twig" to see some examples. Your engine needs to extend the abstract class "TemplateEngine" and implement some methods.
108
114
```php
109
115
class TemplateEngineMyEngine extends TemplateEngine implements Module, ConfigurableModule
0 commit comments