|
1 | 1 | # Template Engine Factory Documentation |
2 | 2 |
|
| 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 | + |
3 | 13 | ## Updating from `1.x` to `2.x` |
4 | 14 |
|
| 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 | + |
5 | 85 | ## Hooks |
6 | 86 |
|
7 | | -### `___shouldRenderPage` |
| 87 | +The TemplateEngineFactory provides several hooks to influence the automatic page rendering process. |
| 88 | + |
8 | 89 | ### `___resolveTemplate` |
9 | | -### `___getTemplateVariables` |
10 | 90 |
|
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 |
12 | 143 |
|
13 | 144 | ## Implementing a Template Engine |
14 | 145 |
|
15 | | -## Executing Tests |
| 146 | +## Running Tests |
| 147 | + |
0 commit comments