diff --git a/composer.lock b/composer.lock index 403f8a0..b0c6b08 100644 --- a/composer.lock +++ b/composer.lock @@ -1587,12 +1587,12 @@ "source": { "type": "git", "url": "https://github.com/symfony/config.git", - "reference": "5d1d799de46fb2f1c5903dae5ded9df8cc991dc8" + "reference": "98d7805fa31752bade23c01e9d2e8a9b90d20181" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/config/zipball/5d1d799de46fb2f1c5903dae5ded9df8cc991dc8", - "reference": "5d1d799de46fb2f1c5903dae5ded9df8cc991dc8", + "url": "https://api.github.com/repos/symfony/config/zipball/98d7805fa31752bade23c01e9d2e8a9b90d20181", + "reference": "98d7805fa31752bade23c01e9d2e8a9b90d20181", "shasum": "" }, "require": { @@ -1642,7 +1642,7 @@ ], "description": "Symfony Config Component", "homepage": "https://symfony.com", - "time": "2018-06-25T17:06:32+00:00" + "time": "2018-07-26T11:25:51+00:00" }, { "name": "symfony/console", @@ -1650,12 +1650,12 @@ "source": { "type": "git", "url": "https://github.com/symfony/console.git", - "reference": "42a0adc7dd656ca2e360285eb6d822df9ce0b160" + "reference": "0c1fcbb9afb5cff992c982ff99c0434f0146dcfc" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/console/zipball/42a0adc7dd656ca2e360285eb6d822df9ce0b160", - "reference": "42a0adc7dd656ca2e360285eb6d822df9ce0b160", + "url": "https://api.github.com/repos/symfony/console/zipball/0c1fcbb9afb5cff992c982ff99c0434f0146dcfc", + "reference": "0c1fcbb9afb5cff992c982ff99c0434f0146dcfc", "shasum": "" }, "require": { @@ -1703,7 +1703,7 @@ ], "description": "Symfony Console Component", "homepage": "https://symfony.com", - "time": "2018-07-09T12:58:09+00:00" + "time": "2018-07-26T11:13:39+00:00" }, { "name": "symfony/contracts", @@ -1825,12 +1825,12 @@ "source": { "type": "git", "url": "https://github.com/symfony/filesystem.git", - "reference": "87d17aa4b6ed33ed85a7ae3442f3c7ba3d8beaf8" + "reference": "15754f6a1ccc88b14be99092299338004a4c8575" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/filesystem/zipball/87d17aa4b6ed33ed85a7ae3442f3c7ba3d8beaf8", - "reference": "87d17aa4b6ed33ed85a7ae3442f3c7ba3d8beaf8", + "url": "https://api.github.com/repos/symfony/filesystem/zipball/15754f6a1ccc88b14be99092299338004a4c8575", + "reference": "15754f6a1ccc88b14be99092299338004a4c8575", "shasum": "" }, "require": { @@ -1867,7 +1867,7 @@ ], "description": "Symfony Filesystem Component", "homepage": "https://symfony.com", - "time": "2018-05-31T10:18:23+00:00" + "time": "2018-07-26T11:25:51+00:00" }, { "name": "symfony/finder", @@ -1875,12 +1875,12 @@ "source": { "type": "git", "url": "https://github.com/symfony/finder.git", - "reference": "31e8fe0c8e55ec05508c3f85e85f11e4b094ca9d" + "reference": "e85fcc2297c61b09193d17ac606413cedf2092c7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/finder/zipball/31e8fe0c8e55ec05508c3f85e85f11e4b094ca9d", - "reference": "31e8fe0c8e55ec05508c3f85e85f11e4b094ca9d", + "url": "https://api.github.com/repos/symfony/finder/zipball/e85fcc2297c61b09193d17ac606413cedf2092c7", + "reference": "e85fcc2297c61b09193d17ac606413cedf2092c7", "shasum": "" }, "require": { @@ -1916,7 +1916,7 @@ ], "description": "Symfony Finder Component", "homepage": "https://symfony.com", - "time": "2018-07-16T12:56:42+00:00" + "time": "2018-07-26T11:25:51+00:00" }, { "name": "symfony/polyfill-ctype", @@ -2041,12 +2041,12 @@ "source": { "type": "git", "url": "https://github.com/symfony/stopwatch.git", - "reference": "e656f8243ff25f65737cd1aa368fe9cf84855b5e" + "reference": "6037c66d94119fb76907c461a4bfb3a31e74ae66" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/stopwatch/zipball/e656f8243ff25f65737cd1aa368fe9cf84855b5e", - "reference": "e656f8243ff25f65737cd1aa368fe9cf84855b5e", + "url": "https://api.github.com/repos/symfony/stopwatch/zipball/6037c66d94119fb76907c461a4bfb3a31e74ae66", + "reference": "6037c66d94119fb76907c461a4bfb3a31e74ae66", "shasum": "" }, "require": { @@ -2083,7 +2083,7 @@ ], "description": "Symfony Stopwatch Component", "homepage": "https://symfony.com", - "time": "2018-07-13T17:06:58+00:00" + "time": "2018-07-26T11:01:15+00:00" }, { "name": "symfony/yaml", diff --git a/docs/README.md b/docs/README.md index 031373e..41bdb62 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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 diff --git a/src/TextTemplate.php b/src/TextTemplate.php index 64e1b52..56873e7 100644 --- a/src/TextTemplate.php +++ b/src/TextTemplate.php @@ -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; + }; } /** @@ -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; @@ -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]; } @@ -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; } diff --git a/test/unit/tpls/15_print/_in.php b/test/unit/tpls/15_print/_in.php new file mode 100644 index 0000000..7505abd --- /dev/null +++ b/test/unit/tpls/15_print/_in.php @@ -0,0 +1,7 @@ + [ + "a", "b", "c" + ] +]; \ No newline at end of file diff --git a/test/unit/tpls/15_print/_in.txt b/test/unit/tpls/15_print/_in.txt new file mode 100644 index 0000000..633dc1d --- /dev/null +++ b/test/unit/tpls/15_print/_in.txt @@ -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: \ No newline at end of file diff --git a/test/unit/tpls/15_print/out.txt b/test/unit/tpls/15_print/out.txt new file mode 100644 index 0000000..3dee823 --- /dev/null +++ b/test/unit/tpls/15_print/out.txt @@ -0,0 +1,14 @@ +1: + 2: + 3: + 4: + + AAA + 5: + + BBB + BBB + 6: + + CCC + 7: \ No newline at end of file diff --git a/test/value-redirection.phpt b/test/value-redirection.phpt new file mode 100644 index 0000000..90a6c4c --- /dev/null +++ b/test/value-redirection.phpt @@ -0,0 +1,58 @@ + out1} + AAA +{/print} +-B- +{print >> out2} + BBB +{/print} +{print >> out2} + BBB +{/print} +-C- +{= out1} +-D- +{= out2} +EOT; + +$compare = <<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"])); \ No newline at end of file