PHP code example of simsoft / twig
1. Go to this page and download the library: Download simsoft/twig 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/ */
simsoft / twig example snippets
Simsoft\Twig\Twig;
$twig = new Twig([
'path' => 'path/to/templates',
'fileExtension' => '.twig',
'debug' => true, // default is false
'charset' => 'UTF-8',
'cache' => 'path/to/cache',
'extensions' => [new \App\MyExtension()],
'namespaces' => [
'name' => '/path/to/template',
],
]);
$twig->display('template_name', ['name' => 'John']);
namespace App;
use use Simsoft\Twig\Extension;
class MyExtension extends Extension
{
public function getGlobals() : array
{
return [
'guest_name' => 'John Doe',
];
}
public function init(): void
{
// add filters
$this->addFilter('obj_to_array', fn ($object) => (array)$object);
// add functions
$this->addFunction('dump', fn(...$args) => call_user_func_array('var_dump', $args));
// add test
$this->addTest('red', function ($value) {
if (isset($value->color) && $value->color == 'red') {
return true;
}
if (isset($value->paint) && $value->paint == 'red') {
return true;
}
return false;
});
}
}