Skip to content

Commit

Permalink
append output
Browse files Browse the repository at this point in the history
  • Loading branch information
matthes committed Jul 27, 2018
1 parent 7331124 commit 0b6d09e
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 25 deletions.
40 changes: 20 additions & 20 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,21 @@ $textTemplate->addSection("sectionxy", function ($content, $params, $command, $c
```


## Function return redirection

Append output to a variable.

```
{print >> out}
A
{/print}
{print >> out}
B
{/print}
{= out}
```


## Debugging the Parameters

Expand Down
29 changes: 24 additions & 5 deletions src/TextTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public function __construct ($text="") {
$this->mTemplateText = $text;
$this->mFilter = self::$__DEFAULT_FILTER;
$this->mFunctions = self::$__DEFAULT_FUNCTION;
$this->sections["print"] = function ($content) {
return $content;
};
}

/**
Expand Down Expand Up @@ -494,7 +497,13 @@ private function _runSection($command, &$context, $content, $cmdParam, $softFail
$content, $funcParams["paramArr"], $command, $context, $cmdParam
);
if ($funcParams["retAs"] !== null) {
$context[$funcParams["retAs"]] = $out;
if ($funcParams["append"]) {
if ( ! isset ($context[$funcParams["retAs"]]))
$context[$funcParams["retAs"]] = "";
$context[$funcParams["retAs"]] .= $out;
} else {
$context[$funcParams["retAs"]] = $out;
}
return "";
} else {
return $out;
Expand Down Expand Up @@ -524,11 +533,15 @@ private function _parseFunctionParameters ($cmdParam, &$context, $softFail)
$exAs = $matches[1];
}, $cmdParamRest);

$cmdParamRest = preg_replace_callback("/\>\s*([a-z0-9\_]+)/i", function ($matches) use (&$retAs) {
$retAs = $matches[1];
$append = false;
$cmdParamRest = preg_replace_callback("/(\>|\>\>)\s*([a-z0-9\_]+)/i", function ($matches) use (&$retAs, &$append) {
if ($matches[1] == ">>") {
$append = true;
}
$retAs = $matches[2];
}, $cmdParamRest);

return ["paramArr" => $paramArr, "retAs" => $retAs, "exAs" => $exAs];
return ["paramArr" => $paramArr, "retAs" => $retAs, "exAs" => $exAs, "append" => $append];
}


Expand Down Expand Up @@ -590,7 +603,13 @@ function ($matches) use (&$context, $softFail) {
$funcParams["paramArr"], $command, $context, $cmdParam
);
if ($funcParams["retAs"] !== null) {
$context[$funcParams["retAs"]] = $out;
if ($funcParams["append"]) {
if ( ! isset ($context[$funcParams["retAs"]]))
$context[$funcParams["retAs"]] = "";
$context[$funcParams["retAs"]] .= $out;
} else {
$context[$funcParams["retAs"]] = $out;
}
} else {
return $out;
}
Expand Down
7 changes: 7 additions & 0 deletions test/unit/tpls/15_print/_in.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [
"arrVal1" => [
"a", "b", "c"
]
];
25 changes: 25 additions & 0 deletions test/unit/tpls/15_print/_in.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
1:
{print > out1}
AAA
{/print}
2:
{print >> out2}
BBB
{/print}
{print >> out2}
BBB
{/print}
3:
{print > out3}
CCC
{/print}
{print > out3}
CCC
{/print}
4:
{=out1}
5:
{=out2}
6:
{=out3}
7:
14 changes: 14 additions & 0 deletions test/unit/tpls/15_print/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
1:
2:
3:
4:

AAA
5:

BBB
BBB
6:

CCC
7:
58 changes: 58 additions & 0 deletions test/value-redirection.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Created by PhpStorm.
* User: matthes
* Date: 25.07.18
* Time: 11:38
*/

namespace Test;
require __DIR__ . "/../vendor/autoload.php";

use http\Exception\InvalidArgumentException;
use Leuffen\TextTemplate\TextTemplate;
use Tester\Assert;
use Tester\Environment;

Environment::setup();


$tpl = <<<EOT
-A-
{print > out1}
AAA
{/print}
-B-
{print >> out2}
BBB
{/print}
{print >> out2}
BBB
{/print}
-C-
{= out1}
-D-
{= out2}
EOT;

$compare = <<<EOT
-A-*AAA*
-B-
-C-
*BBB*
EOT;


$template = new TextTemplate($tpl);
$sec = [];
$template->addSection("section", function ($content, $params, $command, $context, $cmdParam) use (&$sec) {
$sec[$params["name"]] = $content;
if ($command !== "section")
throw new InvalidArgumentException("Command missing");
return "*" . trim ($content) . "*";
});
$textResult = $template->apply([]);

Assert::equal($compare, $textResult);
Assert::equal("AAA", trim($sec["A"]));
Assert::equal("BBB", trim($sec["B"]));

0 comments on commit 0b6d09e

Please sign in to comment.