Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
Back port the changes from contao/core-bundle#580.
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer committed Sep 27, 2016
1 parent cff4c1a commit c5b9e81
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions system/modules/core/library/Contao/StringUtil.php
Expand Up @@ -488,7 +488,8 @@ public static function parseSimpleTokens($strString, $arrData)
$replaceTokens = function ($strSubject) use ($arrData)
{
// Replace tokens
return preg_replace_callback(
return preg_replace_callback
(
'/##([^=!<>\s]+?)##/',
function (array $matches) use ($arrData)
{
Expand Down Expand Up @@ -588,36 +589,47 @@ function (array $matches) use ($arrData)
}
};

// Parsing stack used to keep track of the nesting level
// The last item is true if it is inside a matching if-tag
// The last item is true if it is inside a matching if-tag
$arrStack = [true];

// The last item is true if any if/elseif at that level was true
$arrIfStack = [true];

// Tokenize the string into tag and text blocks
$arrTags = preg_split('/({[^{}]+})\n?/', $strString, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);

// Parse the tokens
foreach ($arrTags as $strTag)
{
// true if it is inside a matching if-tag
// True if it is inside a matching if-tag
$blnCurrent = $arrStack[count($arrStack) - 1];
$blnCurrentIf = $arrIfStack[count($arrIfStack) - 1];

if (strncmp($strTag, '{if', 3) === 0)
{
$arrStack[] = $blnCurrent && $evaluateExpression(substr($strTag, 4, -1));
$blnExpression = $evaluateExpression(substr($strTag, 4, -1));
$arrStack[] = $blnCurrent && $blnExpression;
$arrIfStack[] = $blnExpression;
}
elseif (strncmp($strTag, '{elseif', 7) === 0)
{
$blnExpression = $evaluateExpression(substr($strTag, 8, -1));
array_pop($arrStack);
$arrStack[] = !$blnCurrent && $arrStack[count($arrStack) - 1] && $evaluateExpression(substr($strTag, 8, -1));
array_pop($arrIfStack);
$arrStack[] = !$blnCurrentIf && $arrStack[count($arrStack) - 1] && $blnExpression;
$arrIfStack[] = $blnCurrentIf || $blnExpression;
}
elseif (strncmp($strTag, '{else}', 6) === 0)
{
array_pop($arrStack);
$arrStack[] = !$blnCurrent && $arrStack[count($arrStack) - 1];
array_pop($arrIfStack);
$arrStack[] = !$blnCurrentIf && $arrStack[count($arrStack) - 1];
$arrIfStack[] = true;
}
elseif (strncmp($strTag, '{endif}', 7) === 0)
{
array_pop($arrStack);
array_pop($arrIfStack);
}
elseif ($blnCurrent)
{
Expand Down

0 comments on commit c5b9e81

Please sign in to comment.