opm87/twigbridge is a Laravel package for adds the power of twig to laravel.
It currently has 0 GitHub stars and 2.649 downloads on Packagist (latest version v1.1.3).
Install it with composer require opm87/twigbridge.
Discover more Laravel packages by opm87
or browse all Laravel packages to compare alternatives.
Last updated
Seamlessly implement Twig in Laravel 5.
| Laravel | TwigBridge | | --------- | ---------- | | 5.x | >=0.7 | | 4.2 / 4.1 | 0.6.* | | 4.0 | 0.5.* |
Require this package with Composer:
$ composer require rcrowe/twigbridge
Once Composer has installed or updated your packages you need to register TwigBridge with
Laravel itself. Open up config/app.php and find the providers key, towards the end of
the file, and add TwigBridge\ServiceProvider::class to the end:
'providers' => [
// ...
TwigBridge\ServiceProvider::class,
],
Now find the aliases key, again towards the end of the file, and add
'Twig' => TwigBridge\Facade\Twig::class to have easier access to the TwigBridge
(or Twig_Environment) class:
'aliases' => [
// ...
'Twig' => TwigBridge\Facade\Twig::class,
],
Now that you have both of those lines added to config/app.php we will use Artisan to
publish this package's configuration file:
$ php artisan vendor:publish --provider="TwigBridge\ServiceProvider"
You can begin using Twig like you would any other view:
// resources/views/hello.twig
<h1>{{ 'Hello, world' }}<h1>
// app/Http/routes.php
Route::get('/', function () {
return view('hello');
});
For Lumen, you need to load the same Service Provider, but you have to disable the Auth,
Translator and Url extensions in your local configuration. Copy the config/twigbridge.php
file to your local config folder and register the configuration and Service Provider in
bootstrap/app.php:
$app->configure('twigbridge');
$app->register('TwigBridge\ServiceProvider');
To tell this package to load your Twig files from multiple locations, update the paths array
in config/view.php.
Your Twig files can have any of the file extensions configured in config/twigbridge.php
under the twig.file_extensions key. By default, .html.twig and .twig are supported.
You call the Twig template like you would any other view:
view('i_am_twig', [...]);
TwigBridge also supports views in other packages:
view('pagination::simple');
The above rules continue when extending another Twig template:
{% extends "parent" %}
{% extends "pagination::parent" %}
You can call functions with parameters:
{{ link_to_route('tasks.edit', 'Edit', task.id, {'class': 'btn btn-primary'}) }}
All output variables are escaped by default. Use the raw filter to skip escaping.
{{ some_var }}
{{ html_var | raw }}
{{ long_var | str_limit(50) }}
Sometimes you want to extend or add new functions to use in your Twig templates. To do this,
add a list of extensions for Twig to load to the enabled array in config/twigbridge.php:
'enabled' => [
'TwigBridge\Extensions\Example',
]
TwigBridge supports both a string or a closure as a callback, so for example you might implement the Assetic Twig extension as follows:
'enabled' => [
function ($app) {
$factory = new Assetic\Factory\AssetFactory($app['path'].'/../some/path/');
$factory->setDebug(false);
// etc...
return new Assetic\Extension\Twig\AsseticExtension($factory);
}
]
TwigBridge comes with the following extensions enabled by default:
To enable 0.5.* style Facades, enable the Legacy Facades extension:
TwigBridge\Extension\Laravel\Legacy\FacadesThese loader extensions expose Laravel helpers as both Twig functions and filters. Check
out the config/twigbridge.php file to see a list of defined functions and filters. You
can also add your own.
The FacadeLoader extension allows you to call any facade you have configured in
config/twigbridge.php. This gives your Twig templates integration with any Laravel class
as well as any other classes you alias.
To use the Laravel integration (or indeed any aliased class and method), add your facades
to the config and call them like URL.to(link) (instead of URL::to($link)).
The following helpers/filters are added by the default Extensions. They are based on Laravel's standard helper functions.
asset, action, url, route, secure_url, secure_assetauth_check, auth_guest, auth_usercanconfig_get, config_hasdumpform_* (All the Form::* methods, snake_cased)html_* (All the Html::* methods, snake_cased)input_get, input_old, input_haslink_to, link_to_asset, link_to_route, link_to_actionsession_has, session_get, csrf_token, csrf_field, method_fieldstr_* (All the Str::* methods, snake_cased)trans, trans_choiceurl_* (All the URL::* methods, snake_cased)camel_case, snake_case, studly_casestr_* (All the Str::* methods, snake_cased)trans, trans_choiceapp: the Illuminate\Foundation\Application objecterrors: The $errors MessageBag from the ValidatorTwigBridge also offers two Artisan commands to aid development:
# Empty the Twig cache:
$ php artisan twig:clean
# Lint all Twig templates:
$ php artisan twig:lint