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
$view->set('chunk', $chunk_output); // if you want to set more than a single value, use setMultiple()
97
-
```
98
-
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.
99
-
100
-
### Output markup of a chunk
101
-
102
-
A chunk is a reusable part of a template. That means you can use it multiple times between different template files.
103
-
Furthermore you could put contextual arguments as an array like `array('item' => $item, 'hello' => 'world')` into it.
104
-
105
-
```php
106
-
// in template file: /site/templates/view/template.tpl
The example above loads a template file called "product_partial.tpl" and passes some variables. Calling `render()` returns the rendered markup of the template file.
111
98
112
-
Assume there is installed the module "TemplateEngineTwig" and Twig is chosen as the active template engine. The template file could look like this:
99
+
**NOTE**: Versions >= 1.1.0 of this module provide an API variable `$factory` by default, so the step to get the module is not needed anymore.
113
100
114
-
```html
115
-
// in template file: /site/templates/view/template.twig
116
-
{% for p in nav_pages %}
117
-
{% include 'chunks/nav-item', { 'page': p } %}
118
-
{% endfor %}
119
-
```
120
-
121
-
To render this chunk there must exists two related files. Assuming `chunks/nav-item` this would be:
122
-
123
-
* /site/templates/chunks/nav-item.php
124
-
* /site/templates/view/chunks/nav-item.tpl
101
+
### Chunks
125
102
126
-
If you pass contextual arguments, you can access them inside the `.php`-file by using `$this->key`.
127
-
As you're used to, you can pass key/value pairs to the template using the new API variable `$view`.
A chunk represents a reusable part of a template. It consists of a PHP file containing some logic and a template file which is responsible to render the chunk.
$foo = 'do some logic // something with the contextual data';
143
-
$author = $this->page->createdUser;
144
-
145
-
$view->setMultiple( array(
146
-
'author' => $author,
147
-
'item' => $this->page,
148
-
'foo' => $foo
149
-
));
150
-
```
151
-
152
-
*/site/templates/view/chunks/nav-item.tpl*
153
-
```php
154
-
Author: <?= $author ?> <br />
155
-
<ahref="<?= $item->url ?>"title="<?= $foo ?>">
156
-
<?= $item->title ?>
157
-
</a>
105
+
// The path to the chunk is relative to site/templates without the php suffix
106
+
$chunk = $factory->chunk('chunks/nav-item');
107
+
// You can pass any variables to the chunk, they become available as locally scoped variables
108
+
$chunk->active = true;
109
+
$chunk->set('hidden', false);
110
+
// Returns the markup from the associated template file (view) from the active TemplateEngine. By default, the chunk's template file is looked up at the same path as the chunk file, relative to the storage location of the active template engine, e.g. /site/templates/views/chunks/nav-item.tpl
0 commit comments