Skip to content

Commit 03f8f16

Browse files
committed
snippet params: renderTo, giveTo
1 parent 38dcaca commit 03f8f16

3 files changed

Lines changed: 58 additions & 28 deletions

File tree

assets/plugins/pagebuilder/pagebuilder.php

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class PageBuilder {
44

5-
const version = '1.1.7';
5+
const version = '1.2.0';
66

77
private $modx;
88
private $data;
@@ -154,6 +154,7 @@ public function render($params) {
154154
'templates' => '',
155155
'offset' => 0,
156156
'limit' => 0,
157+
'renderTo' => 'templates',
157158
], $params);
158159

159160
if ($params['blocks'] != '*') {
@@ -165,6 +166,8 @@ public function render($params) {
165166

166167
$this->fetch($params['docid'], $params['container'], false);
167168

169+
$data = [];
170+
168171
foreach ($this->data as $row) {
169172
$idx++;
170173

@@ -188,42 +191,62 @@ public function render($params) {
188191
}
189192

190193
$conf = $this->conf[ $row['config'] ];
191-
$templates = $conf['templates'];
192194

193-
if (!empty($params['templates'])) {
194-
if (!isset($templates[ $params['templates'] ])) {
195-
$out .= "<div>Templates set '" . $params['templates'] . "' not defined</div>";
196-
continue;
195+
$values = $this->prepareData($conf, $row['values']);
196+
197+
if ($params['renderTo'] != 'templates') {
198+
$data[] = $values;
199+
continue;
200+
} else {
201+
$templates = $conf['templates'];
202+
203+
if (!empty($params['templates'])) {
204+
if (!isset($templates[ $params['templates'] ])) {
205+
$out .= "<div>Templates set '" . $params['templates'] . "' not defined</div>";
206+
continue;
207+
}
208+
209+
$templates = $templates[ $params['templates'] ];
197210
}
198211

199-
$templates = $templates[ $params['templates'] ];
200-
}
212+
if (!isset($templates['owner'])) {
213+
$out .= "<div>Template 'owner' not defined</div>";
214+
continue;
215+
}
201216

202-
if (!isset($templates['owner'])) {
203-
$out .= "<div>Template 'owner' not defined</div>";
204-
continue;
217+
$out .= $this->renderFieldsList($templates, $templates['owner'], $conf, $values);
205218
}
219+
}
206220

207-
$values = $this->prepareData($conf, $row['values']);
208-
209-
$out .= $this->renderFieldsList($templates, $templates['owner'], $conf, $values);
221+
if ($params['renderTo'] == 'json') {
222+
$data = json_encode($data, JSON_UNESCAPED_UNICODE);
210223
}
211224

212-
$wrapper = '[+wrap+]';
225+
if ($params['renderTo'] == 'templates') {
226+
$wrapper = '[+wrap+]';
213227

214-
if (!empty($out)) {
215-
if (isset($params['wrapTpl'])) {
216-
$wrapper = $this->modx->getChunk($params['wrapTpl']);
217-
} else if (isset($this->containers[ $params['container'] ])) {
218-
$container = $this->containers[ $params['container'] ];
228+
if (!empty($out)) {
229+
if (isset($params['wrapTpl'])) {
230+
$wrapper = $this->modx->getChunk($params['wrapTpl']);
231+
} else if (isset($this->containers[ $params['container'] ])) {
232+
$container = $this->containers[ $params['container'] ];
219233

220-
if (!empty($container['templates']['owner'])) {
221-
$wrapper = $container['templates']['owner'];
234+
if (!empty($container['templates']['owner'])) {
235+
$wrapper = $container['templates']['owner'];
236+
}
222237
}
223238
}
239+
240+
$out = $this->parseTemplate($wrapper, ['wrap' => $out]);
241+
} else {
242+
$out = $data;
224243
}
225244

226-
return $this->parseTemplate($wrapper, ['wrap' => $out]);
245+
if (!empty($params['giveTo'])) {
246+
return $this->modx->runSnippet($params['giveTo'], ['data' => $out]);
247+
}
248+
249+
return $out;
227250
}
228251

229252
/**
@@ -311,8 +334,6 @@ private function prepareData($options, $values) {
311334
* @return boolean
312335
*/
313336
private function canIncludeBlock($block, $docid) {
314-
$templateid = isset($this->params['template']) ? $this->params['template'] : $this->modx->documentObject['template'];
315-
316337
if (isset($block['placement'])) {
317338
if (isset($this->params['tv']) && $block['placement'] != 'tv') {
318339
return false;
@@ -329,7 +350,7 @@ private function canIncludeBlock($block, $docid) {
329350
}
330351
}
331352

332-
if (isset($block['show_in_templates']) && !in_array($templateid, $block['show_in_templates'])) {
353+
if (isset($block['show_in_templates']) && !in_array($this->params['template'], $block['show_in_templates'])) {
333354
return false;
334355
}
335356

@@ -364,6 +385,15 @@ private function fetch($docid, $containerName = null, $notpl = true) {
364385

365386
$this->conf = [];
366387

388+
if (!isset($this->params['template'])) {
389+
if ($docid == $modx->documentIdentifier) {
390+
$this->params['template'] = $this->modx->documentObject['template'];
391+
} else {
392+
$doc = $this->modx->getDocument($docid, 'template');
393+
$this->params['template'] = $doc['template'];
394+
}
395+
}
396+
367397
// Loading all config files, that complied with filters
368398
foreach (scandir($this->path) as $entry) {
369399
if (pathinfo($entry, PATHINFO_EXTENSION) == 'php') {

install/assets/plugins/PageBuilder.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Creates form for manage content sections
66
*
77
* @category plugin
8-
* @version 1.1.7
8+
* @version 1.2.0
99
* @author mnoskov
1010
* @internal @properties &tabName=Tab name;text;Page Builder &addType=Add type;menu;dropdown,icons,images;dropdown &placement=Placement;menu;content,tab;tab
1111
* @internal @events OnWebPageInit,OnManagerPageInit,OnDocFormRender,OnDocFormSave,OnBeforeEmptyTrash,OnDocDuplicate

install/assets/snippets/PageBuilder.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* output content sections for current page
66
*
7-
* @version 1.1.7
7+
* @version 1.2.0
88
* @author mnoskov
99
* @category snippet
1010
* @internal @properties

0 commit comments

Comments
 (0)