cinam/template-parser

1.1.4 2020-11-04 08:35 UTC

This package is auto-updated.

Last update: 2024-11-04 17:08:34 UTC


README

A simple text parser. It allows you to perform following text transformations:

  • replace placeholders with scalar variables,
  • replace placeholders from array variables using iteration,
  • process logical conditions.

There is no limit for nesting arrays.

Usage

$parser = new \Cinam\TemplateParser\Parser();
$result = $parser->parse($text, $variables);

Examples

Example 1 - Simple variable

$text = 'Hello, {user}!';
$variables = [
    'user' => 'Peter',
];

$parser = new \Cinam\TemplateParser\Parser();
echo $parser->parse($text, $variables);

Hello, Peter!

Example 2 - Condition

$text = <<<EOT
Your score: {score} points.
[IF score > highScore]
  Congratulations, it's the new high score!
[ENDIF]
EOT;
$parser = new \Cinam\TemplateParser\Parser();

echo $parser->parse($text, ['score' => 50, 'highScore' => 60]);

Your score: 50 points.

echo $parser->parse($text, ['score' => 50, 'highScore' => 40]);

Your score: 50 points.

Congratulations, it's the new high score!

Example 3 - Condition with ELSE

$text = '[IF age >= 18]You are an adult[ELSE]Sorry, you are too young[ENDIF]';
$parser = new \Cinam\TemplateParser\Parser();

echo $parser->parse($text, ['age' => 18]);

You are an adult

echo $parser->parse($text, ['age' => 15]);

Sorry, you are too young

Example 4 - Iteration

$text = <<<EOT
Summary:
[START scores]
  {user}, your score is {score}. [IF score > personalBest]Congratulations, it's your personal best![ENDIF]
[END]
EOT;
$variables = [
    'scores' => [
        [
            'user' => 'Peter',
            'score' => 20,
            'personalBest' => 25,
        ],
        [
            'user' => 'Mike',
            'score' => 30,
            'personalBest' => 30,
        ],
        [
            'user' => 'John',
            'score' => 30,
            'personalBest' => 25,
        ],
    ],
];

$parser = new \Cinam\TemplateParser\Parser();
echo $parser->parse($text, $variables);

Summary:

Peter, your score is 20.

Mike, your score is 30.

John, your score is 30. Congratulations, it's your personal best!