2017 © Pedro Peláez
 

library template-parser

Template parser

image

cinam/template-parser

Template parser

  • Monday, January 22, 2018
  • by cinam
  • Repository
  • 1 Watchers
  • 0 Stars
  • 40 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 0 % Grown

The README.md

Template parser

A simple text parser. It allows you to perform following text transformations:, (*1)

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

There is no limit for nesting arrays., (*2)

Usage

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


## Examples ### Example 1 - Simple variable ``` php $text = 'Hello, {user}!'; $variables = [ 'user' => 'Peter', ]; $parser = new \Cinam\TemplateParser\Parser(); echo $parser->parse($text, $variables);

Hello, Peter!, (*4)

Example 2 - Condition

``` php $text = << highScore] Congratulations, it's the new high score! [ENDIF] EOT; $parser = new \Cinam\TemplateParser\Parser();, (*5)

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

*Your score: 50 points.*
``` php
echo $parser->parse($text, ['score' => 50, 'highScore' => 40]);

Your score: 50 points., (*7)

Congratulations, it's the new high score!, (*8)

Example 3 - Condition with ELSE

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

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

*You are an adult*
``` php
echo $parser->parse($text, ['age' => 15]);

Sorry, you are too young, (*11)

Example 4 - Iteration

``` php $text = << 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, ], ], ];, (*12)

$parser = new \Cinam\TemplateParser\Parser(); echo $parser->parse($text, $variables); ``` Summary:, (*13)

Peter, your score is 20., (*14)

Mike, your score is 30., (*15)

John, your score is 30. Congratulations, it's your personal best!, (*16)

The Versions

22/01 2018

dev-refactoring

dev-refactoring http://github.com/cinam/template-parser

Template parser

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Avatar cinam

template parser document parser

19/12 2017

dev-master

9999999-dev http://github.com/cinam/template-parser

Template parser

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Avatar cinam

template parser document parser

19/12 2017

1.0.4

1.0.4.0 http://github.com/cinam/template-parser

Template parser

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Avatar cinam

template parser document parser

18/12 2017

1.0.3

1.0.3.0 http://github.com/cinam/template-parser

Template parser

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Avatar cinam

template parser document parser

14/12 2017

1.0.2

1.0.2.0 http://github.com/cinam/template-parser

Template parser

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Avatar cinam

template parser document parser

14/12 2017

1.0.1

1.0.1.0 http://github.com/cinam/template-parser

Template parser

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Avatar cinam

template parser document parser

04/12 2017

1.0.0

1.0.0.0 http://github.com/cinam/template-parser

Template parser

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar cinam

template parser document parser