Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions core/src/Revolution/modTemplateVar.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class modTemplateVar extends modElement
public $bindings = [
'FILE',
'CHUNK',
'SNIPPET',
'DOCUMENT',
'RESOURCE',
'SELECT',
Expand Down Expand Up @@ -832,12 +833,13 @@ public function getBindingDataFromValue($value)
$nvalue = trim($value);
$cmd = false;
$param = '';
$properties = [];
if (substr($nvalue, 0, 1) == '@') {
list($cmd, $param) = $this->parseBinding($nvalue);
list($cmd, $param, $properties) = $this->parseBinding($nvalue);
$cmd = trim($cmd);
}

return ['cmd' => $cmd, 'param' => $param];
return ['cmd' => $cmd, 'param' => $param, 'properties' => $properties];
}

/**
Expand Down Expand Up @@ -870,6 +872,7 @@ public function processBindings($value = '', $resourceId = 0, $preProcess = true
}
$cmd = $bdata['cmd'];
$param = !empty($bdata['param']) ? $bdata['param'] : null;
$properties = !empty($bdata['properties']) ? $bdata['properties'] : [];
switch ($cmd) {
case 'FILE':
if ($preProcess) {
Expand All @@ -879,9 +882,15 @@ public function processBindings($value = '', $resourceId = 0, $preProcess = true

case 'CHUNK': /* retrieve a chunk and process it's content */
if ($preProcess) {
$output = $this->xpdo->getChunk($param);
$output = $this->xpdo->getChunk($param, $properties);
}
break;

case 'SNIPPET':
if ($preProcess) {
$output = $this->xpdo->runSnippet($param, $properties);
}
break;

case 'RESOURCE':
case 'DOCUMENT': /* retrieve a document and process it's content */
Expand Down Expand Up @@ -984,13 +993,33 @@ public function processBindings($value = '', $resourceId = 0, $preProcess = true
public function parseBinding($binding_string)
{
$match = [];
$match2 = [];
$binding_string = trim($binding_string);
$regexp = '/@(' . implode('|', $this->bindings) . ')\s*(.*)/is'; /* Split binding on whitespace */

if (preg_match($regexp, $binding_string, $match)) {
/* We can't return the match array directly because the first element is the whole string */

$regexp2 = '/(\S+)\s+(.+)/is'; /* Split binding on second whitespace to get properties */

$properties = [];
if (preg_match($regexp2, $match[2] , $match2)) {
if (isset($match2[2])) {
$props = json_decode($match2[2],true);
$valid = json_last_error() === JSON_ERROR_NONE;
if ($valid && is_array($props)){
$properties = $props;
$match[2] = $match2[1];
} else {
$this->xpdo->log(modX::LOG_LEVEL_ERROR, 'modTemplateVar::parseBinding - third parameter is invalid JSON :' . $binding_string);
}
}
}

$binding_array = [
strtoupper($match[1]),
trim($match[2]),
$properties
]; /* Make command uppercase */

return $binding_array;
Expand Down