1. Go to this page and download the library: Download daycry/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/ */
daycry / twig example snippets
$config->toolbarMinimal = true; // only essential metrics
// Same instance as Services::twig()
$twig = twig_instance();
$twig->display('file.html', []);
// Render-and-return / render-and-echo without resolving the service by hand
$html = twig_render('emails/welcome', ['name' => 'Daycry']);
twig_display('layout/main', ['title' => 'Home']);
// Capture stdout from a callable as a string
$captured = twig_capture(static fn () => twig_display('partials/sidebar'));
$config->leanMode = true;
$config->enableDiscoverySnapshot = true; // keep snapshot for faster discovery
$config->enableWarmupSummary = true; // record last warmup result
use Twig\\Loader\\ArrayLoader;
use Daycry\\Twig\\Twig;
$twig = new Twig();
$twig->withLoader(new ArrayLoader([
'hello.twig' => 'Hello {{ name }}'
]));
echo $twig->render('hello', ['name' => 'World']);
$config = new \\Daycry\\Twig\\Config\\Twig();
$config->strictVariables = true;
$twig = new Twig($config);
use Daycry\Twig\Twig;
use App\Twig\MyExtension; // extends \Twig\Extension\AbstractExtension
$twig = new Twig();
$twig->registerExtension(MyExtension::class); // queued or immediate
// Invalidate all templates under namespace '@admin'
$twig->invalidateNamespace('@admin');
// Invalidate all templates in the main (root) namespace
$twig->invalidateNamespace(null);
// Specific templates (logical names without extension)
$summary = $twig->warmup(['welcome','emails/welcome']);
// All discovered templates under configured paths
$summaryAll = $twig->warmupAll();
/* Returned structure:
[
'compiled' => 5,
'skipped' => 12, // already compiled
'errors' => 0,
]
*/
// Force recompilation ignoring existing compiled cache fingerprints
$twig->warmup(['welcome'], true);
use Psr\Log\LoggerInterface;
/** @var LoggerInterface $logger */
$twig = new \Daycry\Twig\Twig($config, $logger);
// or later:
$twig->setLogger($logger);
$twig->setLogger(null); // restore the log_message() fallback
use Daycry\Twig\Constants\TwigEvent;
event(TwigEvent::WarmupAfter->value, $payload);
// All templates
$all = $twig->listTemplates();
// All templates inside a namespace
$admin = $twig->listTemplates(false, '@admin');
// Pattern filtering (within namespace)
$adminDash = $twig->listTemplates(false, '@admin', 'dash*/index');
// With compiled status
$withStatus = $twig->listTemplates(true);