Skip to content

Commit 58e9d81

Browse files
committed
Merge pull request #1 from justonestep/develop
Adds setMultiple and renderChunk methods
2 parents 0bbfc76 + 91597cf commit 58e9d81

6 files changed

Lines changed: 162 additions & 9 deletions

README.md

100644100755
Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ if ($input->post->form) {
4343
$view->set('foo', 'bar');
4444
$view->set('show_nav', true);
4545
$view->set('nav_pages', $pages->get('/')->children());
46+
47+
// Or:
48+
$view->setMultiple(array(
49+
'foo' => 'bar',
50+
'show_nav' => true,
51+
'nav_pages' => $pages->get('/')->children()
52+
));
4653
```
4754
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:
4855
```php
@@ -86,10 +93,70 @@ $chunk = $factory->load('chunks/product_chunk.tpl');
8693
$chunk->set('product_title', $page->title);
8794
$chunk->set('date', date('d.m.Y'));
8895
$chunk_output = $chunk->render();
89-
$view->set('chunk', $chunk_output);
96+
$view->set('chunk', $chunk_output); // if you want to set more than a single value, use setMultiple()
9097
```
9198
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.
9299

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
107+
<?php foreach ($nav_pages as $p): ?>
108+
<?php $page->renderChunk('chunks/nav-item', array('page' => $p)); ?>
109+
<?php endforeach; ?>
110+
```
111+
112+
Assume there is installed the module "TemplateEngineTwig" and Twig is chosen as the active template engine. The template file could look like this:
113+
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
125+
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`.
128+
129+
#### Example: How to use a chunk
130+
131+
*In template file: /site/templates/view/template.twig*
132+
```php
133+
<?php foreach ($nav_pages as $p): ?>
134+
<?php $page->renderChunk('chunks/nav-item', array('page' => $p)); ?>
135+
<?php endforeach; ?>
136+
```
137+
138+
*/site/templates/chunks/nav-item.php*
139+
```php
140+
<?php namespace ProcessWire;
141+
142+
$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+
<a href="<?= $item->url ?>" title="<?= $foo ?>">
156+
<?= $item->title ?>
157+
</a>
158+
```
159+
93160
## Important: Caching
94161
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".
95162

@@ -125,6 +192,11 @@ class TemplateEngineMyEngine extends TemplateEngine implements Module, Configura
125192
{
126193
// Pass a key/value pair to your engine
127194
}
195+
196+
public function setMultiple($pairs = array())
197+
{
198+
// Pass multiple key/value pairs to your engine
199+
}
128200

129201
public function render()
130202
{

TemplateEngine.php

100644100755
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ public function __set($key, $value)
8383
$this->set($key, $value);
8484
}
8585

86+
/**
87+
* @param array $pairs
88+
*/
89+
public function __setMultiple($pairs = array())
90+
{
91+
if (is_array($pairs)) {
92+
foreach ($pairs as $key => $value) {
93+
$this->template->set($key, $value);
94+
}
95+
}
96+
}
97+
8698

8799
/**
88100
* Set a key/value pair to the template engine
@@ -93,6 +105,14 @@ public function __set($key, $value)
93105
abstract public function set($key, $value);
94106

95107

108+
/**
109+
* Set multiple key/value pairs to the template engine
110+
*
111+
* @param array $pairs
112+
*/
113+
abstract public function setMultiple($pairs = array());
114+
115+
96116
/**
97117
* Render markup from template engine
98118
*
@@ -233,4 +253,4 @@ protected function initConfig()
233253
self::$loaded_config[$this->className] = array_merge($this->getDefaultConfig(), $configs);
234254
}
235255
}
236-
}
256+
}

TemplateEngineCache.php

100644100755
File mode changed.

TemplateEngineFactory.module

100644100755
Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,50 @@ class TemplateEngineFactory extends WireData implements Module, ConfigurableModu
7575
}
7676
$this->wire($this->get('api_var'), $engine);
7777
$this->addHookAfter('Page::render', $this, 'hookRender');
78+
$this->addHook('Page::renderChunk', $this, 'renderChunk');
7879
// If the engine supports caching, attach hooks to clear the cache when saving/deleting pages
7980
if (in_array('TemplateEngineCache', class_implements($engine))) {
8081
$this->pages->addHookAfter('save', $this, 'hookClearCache');
8182
$this->pages->addHookAfter('delete', $this, 'hookClearCache');
8283
}
8384
}
8485

86+
/**
87+
* Hook callback for Page::renderChunk
88+
*
89+
* This method is added as a hook to each \Page instance.
90+
* The first argument passed must be the path to the chunk file render
91+
* (relative to wire('config')->paths->templates)
92+
*
93+
* You may pass an array of additional arguments as a second argument
94+
* which will be passed as contextual data to the chunk
95+
*
96+
* @param HookEvent $event
97+
*/
98+
public function renderChunk(HookEvent $event) {
99+
$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+
106+
$context = array_slice($event->arguments(), 1, null, false);
107+
$engine = $this->getInstanceById($this->get('engine'), $chunkFile, true);
108+
if (is_null($engine)) return; // template file is missing
109+
110+
$chunkBaseFile = $this->config->paths->templates . $chunkFile . '.' . $this->config->templateExtension;
111+
if (!file_exists($chunkBaseFile)) return;
112+
$tplFile = new \ProcessWire\TemplateFile($chunkBaseFile);
113+
if (is_array($event->arguments(1))) {
114+
foreach ($event->arguments(1) as $key => $value) $tplFile->$key = $value;
115+
}
116+
echo $tplFile->render();
117+
118+
$view = $this->wire('view', $engine);
119+
echo $view->render();
120+
}
121+
85122

86123
/**
87124
* Method executed after Page::render()
@@ -206,8 +243,9 @@ class TemplateEngineFactory extends WireData implements Module, ConfigurableModu
206243
}
207244
$engine->initEngine();
208245
if ($as_api_var) {
209-
$this->wire($this->get('api_var'), $engine);
246+
$this->wire($this->get('api_var'), $engine);
210247
}
248+
211249
return $engine;
212250
}
213251

TemplateEngineNull.php

100644100755
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ public function initEngine() {}
3030
public function set($key, $value) {}
3131

3232

33+
/**
34+
* Set multiple key/value pairs to the template
35+
*
36+
* @param array $pairs
37+
*/
38+
public function setMultiple($pairs = array()) {}
39+
40+
3341
/**
3442
* Render markup from template file
3543
*
@@ -40,4 +48,4 @@ public function render()
4048
return '';
4149
}
4250

43-
}
51+
}

TemplateEngineProcesswire.module

100644100755
Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ require_once('TemplateEngine.php');
1212
class TemplateEngineProcesswire extends TemplateEngine implements Module, ConfigurableModule
1313
{
1414

15-
/**
16-
* @var TemplateFile
17-
*/
18-
protected $template;
15+
/**
16+
* @var \TemplateFile $template
17+
*/
18+
protected static $template;
1919

2020

2121
/**
@@ -60,6 +60,21 @@ class TemplateEngineProcesswire extends TemplateEngine implements Module, Config
6060
}
6161

6262

63+
/**
64+
* Set multiple key/value pairs to the template
65+
*
66+
* @param array $pairs
67+
*/
68+
public function setMultiple($pairs = array())
69+
{
70+
if (is_array($pairs)) {
71+
foreach ($pairs as $key => $value) {
72+
$this->template->set($key, $value);
73+
}
74+
}
75+
}
76+
77+
6378
/**
6479
* Render markup from template file
6580
*
@@ -109,4 +124,4 @@ class TemplateEngineProcesswire extends TemplateEngine implements Module, Config
109124
return $wrapper;
110125
}
111126

112-
}
127+
}

0 commit comments

Comments
 (0)