Package Data | |
---|---|
Maintainer Username: | winglian |
Maintainer Contact: | hello@vivalacrowe.com (Rob Crowe) |
Package Create Date: | 2013-02-16 |
Package Last Update: | 2014-11-03 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-19 03:11:54 |
Package Statistics | |
---|---|
Total Downloads: | 14 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 0 |
Total Watchers: | 3 |
Total Forks: | 0 |
Total Open Issues: | 0 |
Allows you to use Twig seamlessly in Laravel 4.
TwigBridge >=0.6 requires PHP 5.4+ & Laravel 4.1+.
If you need to support PHP 5.3 or Laravel 4.0 checkout out TwigBridge 0.5.*
Add rcrowe\twigbridge
as a requirement to composer.json:
{
"require": {
"rcrowe/twigbridge": "0.6.*"
}
}
Update your packages with composer update
or install with composer install
.
Once Composer has installed or updated your packages you need to register TwigBridge with Laravel itself. Open up app/config/app.php and find the providers key towards the bottom and add:
'TwigBridge\ServiceProvider'
You can add the TwigBridge Facade, to have easier access to the TwigBridge (or Twig_Environment).
'Twig' => 'TwigBridge\Facade\Twig',
Twig::addExtension('TwigBridge\Extension\Loader\Functions');
Twig::render('mytemplate', $data);
TwigBridge's configuration file can be extended by creating app/config/packages/rcrowe/twigbridge/config.php
. You can find the default configuration file at vendor/rcrowe/twigbridge/src/config/config.php.
You can quickly publish a configuration file by running the following Artisan command.
$ php artisan config:publish rcrowe/twigbridge
You call the Twig template like you would any other view:
// Without the file extension
View::make('i_am_twig', [...])
TwigBridge also supports views in other packages:
View::make('pagination::simple')
The above rules continue when extending another Twig template:
{% extend "parent" %}
{% extend "pagination::parent" %}
You can call functions with parameters:
{{ link_to_route('tasks.edit', 'Edit', task.id, {'class': 'btn btn-primary'}) }}
And output variables, 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 / add new functions for use in Twig templates. Add to the enabled
array in config/extensions.php a list of extensions for Twig to load.
'enabled' => array(
'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.x' style Facades, enable the Legacy Facades extension:
These loader extensions exposes Laravel helpers as both Twig functions and filters.
Check out the config/extensions.php file to see a list of defined function / filters. You can also add your own.
The FacadeLoader extension allows you to call any facade you have configured in config/extensions.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), just 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 the helpers and/or facades, so should be self explaining.
Functions:
Filters:
Global variables:
TwigBridge offers a command for CLI Interaction.
Empty the Twig cache:
$ php artisan twig:clean
Lint all Twig templates:
$ php artisan twig:lint