Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
# Conflicts:
#	README.md
#	src/TextTemplate.php
#	test/unit/tpls/07_if_else/_in.txt
#	test/unit/tpls/07_if_else/out.txt
  • Loading branch information
matthes committed Jul 26, 2017
2 parents 210c8e7 + 73ce4b3 commit 024d477
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 50 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ $tplStr = <<<EOT
Hello World {= name }
{if name == "Matthias"}
Hallo {= name | capitalize }
{elseif name == "Jan"}
Hi Buddy
{else}
You are not Matthias
{/if}
Expand Down Expand Up @@ -179,6 +181,18 @@ Goodbye World
{/if}
```

Lists of choices:

```
{if someVarName == "SomeValue"}
Hello World
{elseif someVarName == "OtherValue"}
Hello Moon
{else}
Goodbye World
{/if}
```

## Debugging the Parameters

To see all Parameters passed to the template use:
Expand Down
98 changes: 62 additions & 36 deletions src/TextTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,16 @@ public function addFilter ($filterName, callable $filterFn) {
public function _replaceElseIf ($input) {
$lines = explode("\n", $input);
for ($li=0; $li < count ($lines); $li++) {
$lines[$li] = preg_replace_callback('/\{else\}/im',
$lines[$li] = preg_replace_callback('/\{else(?<nestingLevel>[0-9]+)\}/im',
function ($matches) use (&$nestingIndex, &$indexCounter, &$li) {
return "{/if}{if ::NL_ELSE_FALSE}";
return "{/if{$matches["nestingLevel"]}}{if{$matches["nestingLevel"]} ::NL_ELSE_FALSE}";
},
$lines[$li]
);
$lines[$li] = preg_replace_callback('/\{elseif(?<nestingLevel>[0-9]+)(?<params>.*)\}/im',
function ($matches) use (&$nestingIndex, &$indexCounter, &$li) {

return "{/if{$matches["nestingLevel"]}}{if{$matches["nestingLevel"]} ::NL_ELSE_FALSE {$matches["params"]}}";
},
$lines[$li]
);
Expand Down Expand Up @@ -135,6 +142,16 @@ function ($matches) use (&$nestingIndex, &$indexCounter, &$li) {
$slash = $matches[1];
$tag = $matches[2];
$rest = $matches[3];
if ($tag == "else" || $tag == "elseif"){

if ( ! isset ($nestingIndex["if"]))
throw new \Exception("Line {$li}: 'if' Opening tag not found for else/elseif tag: '{$matches[0]}'");
if (count ($nestingIndex["if"]) == 0)
throw new \Exception("Line {$li}: Nesting level does not match for closing tag: '{$matches[0]}'");
$curIndex = $nestingIndex["if"][count ($nestingIndex["if"])-1];
$out = "{" . $tag . $curIndex[0] . rtrim($rest) . "}";
return $out;
}
if ($slash == "") {
if ( ! isset ($nestingIndex[$tag]))
$nestingIndex[$tag] = [];
Expand Down Expand Up @@ -284,39 +301,47 @@ private function _runIf ($context, $content, $cmdParam, $softFail=TRUE, &$ifCond
//echo $cmdParam;
$doIf = false;

if (trim ($cmdParam) == "::NL_ELSE_FALSE") {
$cmdParam = trim ($cmdParam);
//echo "\n+ $cmdParam " . strpos($cmdParam, "::NL_ELSE_FALSE");
// Handle {else}{elseif} constructions
if ($cmdParam === "::NL_ELSE_FALSE") {
// This is the {else} path of a if construction
if ($ifConditionDidMatch == false)
$doIf = true; // Run the block if

} else {

if ( ! preg_match('/([\"\']?.*?[\"\']?)\s*(==|<|>|!=)\s*([\"\']?.*[\"\']?)/i', $cmdParam, $matches)) {
return "!! Invalid command sequence: '$cmdParam' !!";
if ($ifConditionDidMatch == true) {
return ""; // Do not run else block
}
$cmdParam = "TRUE==TRUE";
} elseif (strpos($cmdParam, "::NL_ELSE_FALSE") === 0) {
// This is a {elseif (condition)} block
if ($ifConditionDidMatch == true) {
return ""; // Do not run ifelse block, if block before succeeded
}

//print_r ($matches);

$comp1 = $this->_getItemValue(trim($matches[1]), $context);
$comp2 = $this->_getItemValue(trim($matches[3]), $context);

//decho $comp1 . $comp2;
$cmdParam = substr($cmdParam, strlen ("::NL_ELSE_FALSE")+1);
} else {
// This is the original {if}
$ifConditionDidMatch = false;
}

if ( ! preg_match('/([\"\']?.*?[\"\']?)\s*(==|<|>|!=)\s*([\"\']?.*[\"\']?)/i', $cmdParam, $matches)) {
return "!! Invalid command sequence: '$cmdParam' !!";
}

switch ($matches[2]) {
case "==":
$doIf = ($comp1 == $comp2);
break;
case "!=":
$doIf = ($comp1 != $comp2);
break;
case "<":
$doIf = ($comp1 < $comp2);
break;
case ">":
$doIf = ($comp1 > $comp2);
break;
}
$comp1 = $this->_getItemValue(trim($matches[1]), $context);
$comp2 = $this->_getItemValue(trim($matches[3]), $context);

switch ($matches[2]) {
case "==":
$doIf = ($comp1 == $comp2);
break;
case "!=":
$doIf = ($comp1 != $comp2);
break;
case "<":
$doIf = ($comp1 < $comp2);
break;
case ">":
$doIf = ($comp1 > $comp2);
break;
}

if ( ! $doIf)
Expand All @@ -339,11 +364,13 @@ public function _runIfElse ($context, $content, $softFail=TRUE, &$ifConditionDid
}


private $ifConditionMatch = [];

private function _parseBlock ($context, $block, $softFail=TRUE) {
// (?!\{): Lookahead Regex: Don't touch double {{
$ifConditionDidMatch = []; // Array with nesting-Levels

$result = preg_replace_callback('/\n?\{(?!=)((?<command>[a-z]+)(?<nestingLevel>[0-9]+))(?<cmdParam>.*?)\}(?<content>.*?)\n?\{\/\1\}/ism',
function ($matches) use ($context, $softFail, &$ifConditionDidMatch) {
function ($matches) use ($context, $softFail) {
$command = $matches["command"];
$cmdParam = $matches["cmdParam"];
$content = $matches["content"];
Expand All @@ -354,9 +381,7 @@ function ($matches) use ($context, $softFail, &$ifConditionDidMatch) {
return $this->_runFor($context, $content, $cmdParam, $softFail);

case "if":
$ifConditionDidMatch[$nestingLevel] = false;
return $this->_runIf ($context, $content, $cmdParam, $softFail, $ifConditionDidMatch[$nestingLevel]);

return $this->_runIf ($context, $content, $cmdParam, $softFail, $this->ifConditionMatch[$nestingLevel]);

default:
return "!! Invalid command: '$command' !!";
Expand Down Expand Up @@ -389,8 +414,9 @@ public function apply ($params, $softFail=TRUE) {
$text = $this->mTemplateText;

$context = $params;
$text = $this->_replaceElseIf($text);

$text = $this->_replaceNestingLevels($text);
$text = $this->_replaceElseIf($text);

$text = $this->_parseBlock($context, $text, $softFail);
$result = $this->_parseValueOfTags($context, $text, $softFail);
Expand Down
18 changes: 15 additions & 3 deletions test/templates.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,28 @@ use Tester\Assert;

\Tester\Environment::setup();

$in = "{ if xyz}{ if zzz}{=value}{ /if}{else}{/if}";
$tt = new TextTemplate();
$out = $tt->_replaceNestingLevels($in);
Assert::equal("{if0 xyz}{if1 zzz}{=value}{/if1}{else0}{/if0}", $out);

$in = "{ if xyz}{ if zzz}{=value}{ /if}{else}{/if}";
$tt = new TextTemplate();
$out = $tt->_replaceElseIf($in);
Assert::equal("{ if xyz}{ if zzz}{=value}{ /if}{/if}{if ::NL_ELSE_FALSE}{/if}", $out);

$in = "{ if xyz}{ if zzz}{=value}{ /if}{/if}";
$in = "{ if0 xyz}{ if1 zzz}{=value}{ /if1}{else0}{/if0}";
$tt = new TextTemplate();
$out = $tt->_replaceNestingLevels($in);
Assert::equal("{if0 xyz}{if1 zzz}{=value}{/if1}{/if0}", $out);
$out = $tt->_replaceElseIf($in);
Assert::equal("{ if0 xyz}{ if1 zzz}{=value}{ /if1}{/if0}{if0 ::NL_ELSE_FALSE}{/if0}", $out);

$in = "{ if0 xyz}{ if1 zzz}{=value}{ /if1}{elseif0 bbb}{/if0}";
$tt = new TextTemplate();
$out = $tt->_replaceElseIf($in);
Assert::equal("{ if0 xyz}{ if1 zzz}{=value}{ /if1}{/if0}{if0 ::NL_ELSE_FALSE bbb}{/if0}", $out);






Expand Down
12 changes: 6 additions & 6 deletions test/unit/tpls/07_if_else/_in.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
1: Start
1: IN = 4
{if boolVal == TRUE}
2: Boolean is true
2: IN
{else}
3: Some Line of Code
3: OUT
{/if}
{if boolVal == FALSE}
4: Boolean is true
4: OUT
{else}
5: Some Line of Code
5: IN
{/if}
6: Some other text
6: IN
9 changes: 4 additions & 5 deletions test/unit/tpls/07_if_else/out.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
1: Start
2: Boolean is true
3: Some Line of Code
5: Some Line of Code
6: Some other text
1: IN = 4
2: IN
5: IN
6: IN
5 changes: 5 additions & 0 deletions test/unit/tpls/08_if_elseif/_in.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
"boolVal" => TRUE
];
14 changes: 14 additions & 0 deletions test/unit/tpls/08_if_elseif/_in.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
1: IN = 4
{if boolVal == TRUE}
2: IN
{elseif boolVal == TRUE}
3: OUT
{/if}
{if boolVal == FALSE}
4: OUT
{elseif boolVal == TRUE}
5: IN
{else}
6: OUT
{/if}
7: IN
4 changes: 4 additions & 0 deletions test/unit/tpls/08_if_elseif/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1: IN = 4
2: IN
5: IN
7: IN
6 changes: 6 additions & 0 deletions test/unit/tpls/08_if_elseif_complex/_in.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
"t1" => TRUE,
"t2" => TRUE
];
16 changes: 16 additions & 0 deletions test/unit/tpls/08_if_elseif_complex/_in.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
1: IN = 4
{if t1 == TRUE}
2: IN
{if t2 == FALSE}
3: OUT
{elseif t2 == TRUE}
4: IN
{else}
5: OUT
{/if}
{elseif t1 == TRUE}
6: OUT
{else}
7: OUT
{/if}
8: IN
4 changes: 4 additions & 0 deletions test/unit/tpls/08_if_elseif_complex/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1: IN = 4
2: IN
4: IN
8: IN

0 comments on commit 024d477

Please sign in to comment.