1. Go to this page and download the library: Download youniwemi/string-template 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/ */
youniwemi / string-template example snippets
$engine = new Youniwemi\StringTemplate\Engine;
//Scalar value: returns "This is my value: nic"
$engine->render("This is my value: {}", 'nic');
//Array value: returns "My name is Nicolò Martini"
$engine->render("My name is {name} {surname}", ['name' => 'Nicolò', 'surname' => 'Martini']);
//Nested array value: returns "My name is Nicolò and her name is Gabriella"
$engine->render(
"My name is {me.name} and her name is {her.name}",
[
'me' => ['name' => 'Nicolò'],
'her' => ['name' => 'Gabriella']
]);
class Foo { function __toString() { return 'foo'; }
//Returns "foo: bar"
$engine->render(
"{val}: bar",
['val' => new Foo]);
$engine = new Youniwemi\StringTemplate\Engine(':', '');
//Returns I am Nicolò Martini
$engine->render(
"I am :name :surname",
[
'name' => 'Nicolò',
'surname' => 'Martini'
]);
$engine = new Youniwemi\StringTemplate\Engine();
//Returns Oh! You
$engine->render(
'Oh! {#name}{test}{/name}',
[
'name' => true,
'test' => 'You'
]);
$engine = new Youniwemi\StringTemplate\Engine();
//Returns Oh! My
$engine->render(
'Oh! {#name}{test}{#else}My{/name}',
[
'name' => false,
'test' => 'You'
]);
$engine = new Youniwemi\StringTemplate\Engine();
//Returns Oh! JOHN
$engine->render(
'Oh! {name|upper}',
[
'name' => 'John'
]);
$engine = new Youniwemi\StringTemplate\Engine();
//Returns Oh! John
$engine->render(
'Oh! {name|upper}',
[
'name' => function() {
return 'John';
}
]);
$engine = new Youniwemi\StringTemplate\Engine();
//Returns Oh! John Doe
$engine->render(
'Oh! {name}',
[
'first' => 'John',
'last' => 'Doe',
'name' => function($values) {
return $values['first'].' '.$values['last'];
}
]);
$engine = new Youniwemi\StringTemplate\Engine;
//Returns I have 1.2 (1.230000E+0) apples.
$engine->render(
"I have {num%.1f} ({num%.6E}) {fruit}.",
[
'num' => 1.23,
'fruit' => 'apples'
]
)