Ejemplo n.º 1
0
<?php

/**
 * Interpolate.php - Simple string interpolation in PHP.
 *
 * @author Lim Yuan Qing <*****@*****.**>
 * @license MIT
 * @link https://github.com/yuanqing/interpolate
 */
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once __DIR__ . '/src/Interpolate.php';
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!'
$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!'
Ejemplo n.º 2
0
 public function testNestedKeys()
 {
     $i = new Interpolate();
     $data = array('foo' => array('bar baz' => array('qux' => 'quux')));
     $this->assertEquals($i->render('{{ foo.bar baz.qux }}', $data), 'quux');
     $this->assertEquals($i->render('{{foo . bar baz . qux}}', $data), 'quux');
     $this->assertEquals($i->render('{{ foo. bar baz .qux }}', $data), 'quux');
 }