PHP code example of cinam / template-parser

1. Go to this page and download the library: Download cinam/template-parser library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cinam / template-parser example snippets

 php
$parser = new \Cinam\TemplateParser\Parser();
$result = $parser->parse($text, $variables);
 php
$text = 'Hello, {user}!';
$variables = [
    'user' => 'Peter',
];

$parser = new \Cinam\TemplateParser\Parser();
echo $parser->parse($text, $variables);
 php
$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]);
 php
echo $parser->parse($text, ['score' => 50, 'highScore' => 40]);
 php
$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]);
 php
echo $parser->parse($text, ['age' => 15]);
 php
$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);