Skip to content

Commit 0f45967

Browse files
authored
Support properties for @CHUNK/@snippet bindings (#15488)
* properties for @chunk and @snippet binding * fix undefined properties * initialize match2 * check for valid JSON
1 parent e503d9d commit 0f45967

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

core/src/Revolution/modTemplateVar.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class modTemplateVar extends modElement
5252
public $bindings = [
5353
'FILE',
5454
'CHUNK',
55+
'SNIPPET',
5556
'DOCUMENT',
5657
'RESOURCE',
5758
'SELECT',
@@ -832,12 +833,13 @@ public function getBindingDataFromValue($value)
832833
$nvalue = trim($value);
833834
$cmd = false;
834835
$param = '';
836+
$properties = [];
835837
if (substr($nvalue, 0, 1) == '@') {
836-
list($cmd, $param) = $this->parseBinding($nvalue);
838+
list($cmd, $param, $properties) = $this->parseBinding($nvalue);
837839
$cmd = trim($cmd);
838840
}
839841

840-
return ['cmd' => $cmd, 'param' => $param];
842+
return ['cmd' => $cmd, 'param' => $param, 'properties' => $properties];
841843
}
842844

843845
/**
@@ -870,6 +872,7 @@ public function processBindings($value = '', $resourceId = 0, $preProcess = true
870872
}
871873
$cmd = $bdata['cmd'];
872874
$param = !empty($bdata['param']) ? $bdata['param'] : null;
875+
$properties = !empty($bdata['properties']) ? $bdata['properties'] : [];
873876
switch ($cmd) {
874877
case 'FILE':
875878
if ($preProcess) {
@@ -879,9 +882,15 @@ public function processBindings($value = '', $resourceId = 0, $preProcess = true
879882

880883
case 'CHUNK': /* retrieve a chunk and process it's content */
881884
if ($preProcess) {
882-
$output = $this->xpdo->getChunk($param);
885+
$output = $this->xpdo->getChunk($param, $properties);
883886
}
884887
break;
888+
889+
case 'SNIPPET':
890+
if ($preProcess) {
891+
$output = $this->xpdo->runSnippet($param, $properties);
892+
}
893+
break;
885894

886895
case 'RESOURCE':
887896
case 'DOCUMENT': /* retrieve a document and process it's content */
@@ -984,13 +993,33 @@ public function processBindings($value = '', $resourceId = 0, $preProcess = true
984993
public function parseBinding($binding_string)
985994
{
986995
$match = [];
996+
$match2 = [];
987997
$binding_string = trim($binding_string);
988998
$regexp = '/@(' . implode('|', $this->bindings) . ')\s*(.*)/is'; /* Split binding on whitespace */
999+
9891000
if (preg_match($regexp, $binding_string, $match)) {
9901001
/* We can't return the match array directly because the first element is the whole string */
1002+
1003+
$regexp2 = '/(\S+)\s+(.+)/is'; /* Split binding on second whitespace to get properties */
1004+
1005+
$properties = [];
1006+
if (preg_match($regexp2, $match[2] , $match2)) {
1007+
if (isset($match2[2])) {
1008+
$props = json_decode($match2[2],true);
1009+
$valid = json_last_error() === JSON_ERROR_NONE;
1010+
if ($valid && is_array($props)){
1011+
$properties = $props;
1012+
$match[2] = $match2[1];
1013+
} else {
1014+
$this->xpdo->log(modX::LOG_LEVEL_ERROR, 'modTemplateVar::parseBinding - third parameter is invalid JSON :' . $binding_string);
1015+
}
1016+
}
1017+
}
1018+
9911019
$binding_array = [
9921020
strtoupper($match[1]),
9931021
trim($match[2]),
1022+
$properties
9941023
]; /* Make command uppercase */
9951024

9961025
return $binding_array;

0 commit comments

Comments
 (0)