Twig is a template language for PHP.
Twig uses a syntax similar to the Django and Jinja template languages which inspired the Twig runtime environment.
Use the package with composer install
> composer require daycry/twig
Run command:
> php spark twig:publish
This command will copy a config file to your app namespace.
Then you can adjust it to your needs. By default file will be present in app/Config/Twig.php
.
$twig = new \Daycry\Twig\Twig();
$twig->display( 'file.html', [] );
$twig = \Config\Services::twig();
$twig->display( 'file.html', [] );
In your BaseController - $helpers array, add an element with your helper filename.
protected $helpers = [ 'twig_helper' ];
And then you can use the helper
$twig = twig_instance();
$twig->display( 'file.html', [] );
$twig = new \Daycry\Twig\Twig();
$session = \Config\Services::session();
$session->set( array( 'name' => 'Daycry' ) );
$twig->addGlobal( 'session', $session );
$twig->display( 'file.html', [] );
<!DOCTYPE html>
<html lang="es">
<head>
<title>Example</title>
<meta charset="UTF-8">
<meta name="title" content="Example">
<meta name="description" content="Example">
</head>
<body>
<h1>Hi {{ name }}</h1>
{{ dump( session.get( 'name' ) ) }}
</body>
</html>
If you want to debug the data in twig templates.
Toolbar.php file
use Daycry\Twig\Debug\Toolbar\Collectors\Twig;
public array $collectors = [
...
//Views::class,
Twig::class
];