StringTemplate icon indicating copy to clipboard operation
StringTemplate copied to clipboard

Support for conditional statements

Open Donnie opened this issue 7 years ago • 4 comments

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}.

Donnie avatar Aug 23 '17 07:08 Donnie

This would be great. Have you found a solution?

bnlab avatar Jan 15 '18 11:01 bnlab

Hoa\Ruler could help you in this circumstance :)

https://github.com/hoaproject/Ruler

drwxmrrs avatar Apr 18 '18 22:04 drwxmrrs

@bnlab @Donnie

Conditional can be rendered using PHP

  • this approach is acceptable if the conditional can be specified in PHP capture

Alternatively conditional can be rendered using JMESPATH

  • this approach is acceptable if the conditional must be provided as a string capture

dreftymac avatar Jan 09 '19 14:01 dreftymac

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'
    ]);

rahal avatar Mar 13 '23 14:03 rahal