StringTemplate
StringTemplate copied to clipboard
Support for conditional statements
I have been looking for a script which would also process conditional statements in string.
for e.g: $string = 'My name is {name} and I scored {if: score>10 then well else poorly}.
This would be great. Have you found a solution?
Hoa\Ruler could help you in this circumstance :)
https://github.com/hoaproject/Ruler
@bnlab @Donnie
Conditional can be rendered using PHP
- this approach is acceptable if the conditional can be specified in PHP
Alternatively conditional can be rendered using JMESPATH
- this approach is acceptable if the conditional must be provided as a string
Hi, old issue here, me too, I needed support for conditionnal and I added it : here is my pull request for this feature ( as well as some other improvements ) : https://github.com/nicmart/StringTemplate/pull/22
You can use a simple condition:
$engine = new StringTemplate\Engine();
//Returns Oh! You
$engine->render(
'Oh! {#name}{test}{/name}',
[
'name' => true,
'test' => 'You'
]);
You can use a simple condition with else:
$engine = new StringTemplate\Engine();
//Returns Oh! My
$engine->render(
'Oh! {#name}{test}{#else}My{/name}',
[
'name' => false,
'test' => 'You'
]);