Skip to content

Commit caebd28

Browse files
author
Tabea David
committed
adds renderChunk and setMultiple
1 parent 0bbfc76 commit caebd28

5 files changed

Lines changed: 83 additions & 9 deletions

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: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,42 @@ 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 as 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 arbitrary amount of additional arguments which will
94+
* be passed as contextual data to the chunk data provider.
95+
*
96+
* @param HookEvent $event
97+
* @see \nw\DataProviders\ChunkDataProvider
98+
*/
99+
public function renderChunk(HookEvent $event) {
100+
$chunkFile = $event->arguments(0);
101+
$context = array_slice($event->arguments(), 1, null, false);
102+
$engine = $this->getInstanceById($this->get('engine'), $chunkFile, true, true);
103+
104+
$tplFile = new \ProcessWire\TemplateFile($this->config->paths->templates . $chunkFile . '.' . $this->config->templateExtension);
105+
if (is_array($event->arguments(1))) {
106+
foreach ($event->arguments(1) as $key => $value) $tplFile->$key = $value;
107+
}
108+
echo $tplFile->render();
109+
110+
$view = $this->wire('view', $engine);
111+
echo $view->render();
112+
}
113+
85114

86115
/**
87116
* Method executed after Page::render()
@@ -187,7 +216,7 @@ class TemplateEngineFactory extends WireData implements Module, ConfigurableModu
187216
* @return TemplateEngine|null
188217
* @throws WireException
189218
*/
190-
public function getInstanceById($class, $filename = '', $as_api_var=false)
219+
public function getInstanceById($class, $filename = '', $as_api_var=false, $chunk=false)
191220
{
192221
$installed = $this->getInstalledEngines();
193222
if (!in_array($class, array_keys($installed))) {
@@ -206,8 +235,9 @@ class TemplateEngineFactory extends WireData implements Module, ConfigurableModu
206235
}
207236
$engine->initEngine();
208237
if ($as_api_var) {
209-
$this->wire($this->get('api_var'), $engine);
238+
$this->wire($this->get('api_var'), $engine);
210239
}
240+
211241
return $engine;
212242
}
213243

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: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
require_once('TemplateEngine.php');
33

4+
45
/**
56
* TemplateEngineProcesswire
67
*
@@ -12,10 +13,10 @@ require_once('TemplateEngine.php');
1213
class TemplateEngineProcesswire extends TemplateEngine implements Module, ConfigurableModule
1314
{
1415

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

2021

2122
/**
@@ -60,6 +61,21 @@ class TemplateEngineProcesswire extends TemplateEngine implements Module, Config
6061
}
6162

6263

64+
/**
65+
* Set multiple key/value pairs to the template
66+
*
67+
* @param array $pairs
68+
*/
69+
public function setMultiple($pairs = array())
70+
{
71+
if (is_array($pairs)) {
72+
foreach ($pairs as $key => $value) {
73+
$this->template->set($key, $value);
74+
}
75+
}
76+
}
77+
78+
6379
/**
6480
* Render markup from template file
6581
*
@@ -109,4 +125,4 @@ class TemplateEngineProcesswire extends TemplateEngine implements Module, Config
109125
return $wrapper;
110126
}
111127

112-
}
128+
}

0 commit comments

Comments
 (0)