You may use composer to install LighcnCandy:
composer require zordius/lightncandy:dev-master
The best practice is compile template into PHP code and save it for later use.
// Use composer autoloader
require_once('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "Welcome {{name}} , You win \${{value}} dollars!!\n";
$phpStr = LightnCandy::compile($template); // set compiled PHP code into $phpStr
// Save the compiled PHP code into a php file
file_put_contents('render.php', '<?php ' . $phpStr . '?>');
// Get the render function from the php file
$renderer = include('render.php');
// Render by different data
echo "Template is:\n$template\n";
echo $renderer(array('name' => 'John', 'value' => 10000));
echo $renderer(array('name' => 'Peter', 'value' => 1000));
Template is:
Welcome {{name}} , You win ${{value}} dollars!!
Welcome John , You win $10000 dollars!!
Welcome Peter , You win $1000 dollars!!
You can use LightnCandy::prepare($phpStr)
to get the render function. This is a deprecated way and should only be used for developing or testing.
// Use composer autoloader
require_once('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "Welcome {{name}} , You win \${{value}} dollars!!\n";
$phpStr = LightnCandy::compile($template); // set compiled PHP code into $phpStr
// Quick and deprecated way to get render function
$renderer = LightnCandy::prepare($phpStr);
// Render by different data
echo "Template is:\n$template\n";
echo $renderer(array('name' => 'John', 'value' => 10000));
Template is:
Welcome {{name}} , You win ${{value}} dollars!!
Welcome John , You win $10000 dollars!!