Skip to content

Commit 8a45e0c

Browse files
author
Tabea David
committed
added error handling to renderChunk and documentation
1 parent caebd28 commit 8a45e0c

2 files changed

Lines changed: 91 additions & 7 deletions

File tree

README.md

100644100755
Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ For each controller that is outputting markup, a corresponding template file sho
3232
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".
3333

3434
The following example uses the ProcessWire template engine:
35+
3536
```php
3637
// In controller file: /site/templates/home.php
3738

@@ -43,8 +44,16 @@ if ($input->post->form) {
4344
$view->set('foo', 'bar');
4445
$view->set('show_nav', true);
4546
$view->set('nav_pages', $pages->get('/')->children());
47+
48+
// Or:
49+
$view->setMultiple(array(
50+
'foo' => 'bar',
51+
'show_nav' => true,
52+
'nav_pages' => $pages->get('/')->children()
53+
));
4654
```
4755
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:
56+
4857
```php
4958
// In template file: /site/templates/view/home.php
5059

@@ -60,6 +69,7 @@ In the example above, some logic is processed if a form was sent. Note that ther
6069
<?php endif; ?>
6170
```
6271
Assume there is installed the module "TemplateEngineSmarty" and Smarty is chosen as the active template engine. The template file could look like this:
72+
6373
```php
6474
// In template file: /site/templates/smarty/home.tpl
6575

@@ -78,6 +88,7 @@ The introduced API variable acts as a gateway to the active template engine. Thi
7888

7989
### Load and output markup of other template files
8090
Use the "TemplateEngineFactory" module to load any template file and output it's markup:
91+
8192
```php
8293
// In controller file: /site/templates/product.php
8394

@@ -86,10 +97,70 @@ $chunk = $factory->load('chunks/product_chunk.tpl');
8697
$chunk->set('product_title', $page->title);
8798
$chunk->set('date', date('d.m.Y'));
8899
$chunk_output = $chunk->render();
89-
$view->set('chunk', $chunk_output);
100+
$view->set('chunk', $chunk_output); // if you want to set more than a single value, use setMultiple()
90101
```
91102
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.
92103

104+
### Output markup of a chunk
105+
106+
A chunk is a reusable part of a template. That means you can use it multiple times between different template files.
107+
Furthermore you could put contextual arguments as an array like `array('item' => $item, 'hello' => 'world')` into it.
108+
109+
```php
110+
// in template file: /site/templates/view/template.tpl
111+
<?php foreach ($nav_pages as $p): ?>
112+
<?php $page->renderChunk('chunks/nav-item', array('page' => $p)); ?>
113+
<?php endforeach; ?>
114+
```
115+
116+
Assume there is installed the module "TemplateEngineTwig" and Twig is chosen as the active template engine. The template file could look like this:
117+
118+
```html
119+
// in template file: /site/templates/view/template.twig
120+
{% for p in nav_pages %}
121+
{% include 'chunks/nav-item', { 'page': p } %}
122+
{% endfor %}
123+
```
124+
125+
To render this chunk there must exists two related files. Assuming `chunks/nav-item` this would be:
126+
127+
* /site/templates/chunks/nav-item.php
128+
* /site/templates/view/chunks/nav-item.tpl
129+
130+
If you pass contextual arguments, you can access them inside the `.php`-file by using `$this->key`.
131+
As you're used to, you can pass key/value pairs to the template using the new API variable `$view`.
132+
133+
#### Example: How to use a chunk
134+
135+
*In template file: /site/templates/view/template.twig*
136+
```php
137+
<?php foreach ($nav_pages as $p): ?>
138+
<?php $page->renderChunk('chunks/nav-item', array('page' => $p)); ?>
139+
<?php endforeach; ?>
140+
```
141+
142+
*/site/templates/chunks/nav-item.php*
143+
```php
144+
<?php namespace ProcessWire;
145+
146+
$foo = 'do some logic // something with the contextual data';
147+
$author = $this->page->createdUser;
148+
149+
$view->setMultiple( array(
150+
'author' => $author,
151+
'item' => $this->page,
152+
'foo' => $foo
153+
));
154+
```
155+
156+
*/site/templates/view/chunks/nav-item.tpl*
157+
```php
158+
Author: <?= $author ?> <br />
159+
<a href="<?= $item->url ?>" title="<?= $foo ?>">
160+
<?= $item->title ?>
161+
</a>
162+
```
163+
93164
## Important: Caching
94165
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".
95166

@@ -125,6 +196,11 @@ class TemplateEngineMyEngine extends TemplateEngine implements Module, Configura
125196
{
126197
// Pass a key/value pair to your engine
127198
}
199+
200+
public function setMultiple($pairs = array())
201+
{
202+
// Pass multiple key/value pairs to your engine
203+
}
128204

129205
public function render()
130206
{

TemplateEngineFactory.module

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,30 @@ class TemplateEngineFactory extends WireData implements Module, ConfigurableModu
8686
/**
8787
* Hook callback for Page::renderChunk
8888
*
89-
* This method as added as a hook to each \Page instance.
89+
* This method is added as a hook to each \Page instance.
9090
* The first argument passed must be the path to the chunk file render
91-
* (relative to wire('config')->paths->templates).
91+
* (relative to wire('config')->paths->templates)
9292
*
93-
* You may pass an arbitrary amount of additional arguments which will
94-
* be passed as contextual data to the chunk data provider.
93+
* You may pass an array of additional arguments as a second argument
94+
* which will be passed as contextual data to the chunk
9595
*
9696
* @param HookEvent $event
97-
* @see \nw\DataProviders\ChunkDataProvider
9897
*/
9998
public function renderChunk(HookEvent $event) {
10099
$chunkFile = $event->arguments(0);
100+
// remove file extension if exists
101+
$pathInfo = pathinfo($chunkFile);
102+
if (array_key_exists('extension', $pathInfo)) {
103+
$chunkFile = $pathInfo['dirname'] . '/' . pathinfo($chunkFile, PATHINFO_FILENAME);
104+
}
105+
101106
$context = array_slice($event->arguments(), 1, null, false);
102107
$engine = $this->getInstanceById($this->get('engine'), $chunkFile, true, true);
108+
if (is_null($engine)) return; // template file is missing
103109

104-
$tplFile = new \ProcessWire\TemplateFile($this->config->paths->templates . $chunkFile . '.' . $this->config->templateExtension);
110+
$chunkBaseFile = $this->config->paths->templates . $chunkFile . '.' . $this->config->templateExtension;
111+
if (!file_exists($chunkBaseFile)) return;
112+
$tplFile = new \ProcessWire\TemplateFile($chunkBaseFile);
105113
if (is_array($event->arguments(1))) {
106114
foreach ($event->arguments(1) as $key => $value) $tplFile->$key = $value;
107115
}

0 commit comments

Comments
 (0)