Install the package via Composer:
composer require vendor/package-name
Publish the configuration (if applicable) and service provider:
php artisan vendor:publish --provider="Vendor\PackageName\PackageServiceProvider"
Register the Twig service provider in config/app.php under providers if not auto-discovered.
First use case: Render a Twig template in a Laravel view:
use Vendor\PackageName\Facades\Twig;
$content = Twig::render('template.twig', ['data' => 'value']);
Check the updated documentation for the single remaining Twig function (twig_function_name).
{{ twig_content(data) }}) in Blade views to embed Twig-rendered content.
Example Blade template:
@twig_content(['key' => 'value'])
@twig('path/to/template', ['var' => $data])
include or embed tags within the single function to modularize templates.try {
$output = Twig::render('template.twig', $data);
} catch (\Vendor\PackageName\Exceptions\TwigException $e) {
Log::error($e->getMessage());
return response()->view('errors.twig', [], 500);
}
'cache' => storage_path('framework/views') in config).'twig' => [
'functions' => [
new \Twig\TwigFunction('custom_func', function($arg) { return strtoupper($arg); }),
],
],
twig_render, twig_partial) are deprecated. Replace them with the new unified function (check docs for exact name).
Migration Tip: Use a regex find/replace in your codebase:
find . -name "*.php" -exec sed -i 's/old_function/new_function/g' {} +
views directory is in your composer.json autoload:
"autoload": {
"files": ["resources/views"]
}
{{ dump(data) }} in Twig templates to debug passed variables.'debug' => env('APP_DEBUG', false),
{% cache %} tags.$this->app->bind(\Twig\Environment::class, function ($app) {
return new \Vendor\PackageName\CustomTwigEnvironment(...);
});
Twig.Render event to modify output or inject data:
Event::listen('Twig.Render', function ($template, $data) {
$data['global_var'] = 'value';
return [$template, $data];
});
config/services.php or config/twig.php.config/caching or environment variables to toggle features (e.g., debug mode):
'debug' => env('TWIG_DEBUG', false),
@auth, @foreach) and Twig for complex templating (e.g., nested loops, filters).How can I help you explore Laravel packages today?