2017 © Pedro Peláez
 

library interpolate

Simple string interpolation in PHP.

image

yuanqing/interpolate

Simple string interpolation in PHP.

  • Friday, September 12, 2014
  • by yuanqing
  • Repository
  • 1 Watchers
  • 4 Stars
  • 1,440 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 7 Versions
  • 7 % Grown

The README.md

Interpolate.php Packagist Version Build Status Coverage Status

A small PHP package for interpolating values from an array into a template string., (*1)

Think of it as a lightweight alternative to Mustache:, (*2)

use yuanqing\Interpolate\Interpolate;

$i = new Interpolate;
$tmpl = '{{ foo.bar }}, {{ foo.baz }}!';
$data = array(
  'foo' => array(
    'bar' => 'Hello',
    'baz' => 'World'
  )
);
$i->render($tmpl, $data); #=> 'Hello, World!'

Usage

  1. Tags are enclosed in double braces., (*3)

  2. Straight-up substitution; there are no conditional blocks, sections and so forth., (*4)

  3. Tags can reference nested values in the multidimensional array (as in the example above)., (*5)

  4. A value to be interpolated can be a scalar, an object that implements __toString(), or a callback that returns a string:, (*6)

    $i = new Interpolate;
    $tmpl = '{{ baz }}';
    $data = array(
      'foo' => 'Hello',
      'bar' => 'World',
      'baz' => function($data) {
        return sprintf('%s, %s!', $data['foo'], $data['bar']);
      }
    );
    $i->render($tmpl, $data); #=> 'Hello, World!'
    

    Note that the first argument of the callback is the $data array., (*7)

  5. If a value for a tag is not found, the tag will be replaced with an empty string., (*8)

The two examples in this README may be found in the examples.php file., (*9)

Requirements

Interpolate.php requires at least PHP 5.3, or HHVM., (*10)

Installation

Install with Composer

  1. Install Composer., (*11)

  2. Install the Interpolate.php Composer package:, (*12)

    $ composer require yuanqing/interpolate ~1.2
    
  3. In your PHP, require the Composer autoloader:, (*13)

    require_once __DIR__ . '/vendor/autoload.php';
    

Install manually

  1. Clone this repository:, (*14)

    $ git clone https://github.com/yuanqing/interpolate
    

    Or just grab the zip., (*15)

  2. In your PHP, require Interpolate.php:, (*16)

    require_once __DIR__ . '/src/Interpolate.php';
    

Testing

You need PHPUnit to run the tests:, (*17)

$ git clone https://github.com/yuanqing/interpolate
$ cd interpolate
$ phpunit

License

MIT license, (*18)

The Versions

12/09 2014

dev-master

9999999-dev https://github.com/yuanqing/interpolate

Simple string interpolation in PHP.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

by Lim Yuan Qing

string regex interpolation interpolate

17/07 2014

v1.2.1

1.2.1.0 https://github.com/yuanqing/interpolate

Simple string interpolation in PHP.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

by Lim Yuan Qing

string regex interpolation interpolate

15/07 2014

v1.2.0

1.2.0.0 https://github.com/yuanqing/interpolate

Simple string interpolation in PHP.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

by Lim Yuan Qing

string interpolation interpolate

14/07 2014

v1.1.0

1.1.0.0 https://github.com/yuanqing/interpolate

Simple string interpolation in PHP.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

by Lim Yuan Qing

string interpolation interpolate

12/07 2014

v1.0.2

1.0.2.0 https://github.com/yuanqing/interpolate

Simple string interpolation in PHP.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

by Lim Yuan Qing

string interpolation interpolate

10/07 2014

v1.0.1

1.0.1.0 https://github.com/yuanqing/interpolate

Simple string interpolation in PHP.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

by Lim Yuan Qing

string interpolation interpolate

10/07 2014

v1.0.0

1.0.0.0 https://github.com/yuanqing/interpolate

Simple string interpolation in PHP.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

by Lim Yuan Qing

string interpolation interpolate