gencer / twigbridge-laravel-4
Adds the power of Twig to Laravel
Requires
- php: >=5.4.0
- illuminate/support: ~4.0
- illuminate/view: ~4.0
- twig/twig: ~1.15
Requires (Dev)
- mockery/mockery: ~0.9
- phpunit/phpunit: ~4.1
- satooshi/php-coveralls: ~0.6
- squizlabs/php_codesniffer: ~1.5.0
Suggests
- twig/extensions: ~1.0
- dev-Branch_0.6.0-beta3
- v0.6.1
- v0.6.0-beta4
- v0.6.0-beta3
- 0.6.0-beta2
- 0.6.0-beta1
- 0.5.4
- 0.5.3
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.1
- 0.4.0
- 0.3.1
- 0.3.0
- 0.2.6
- 0.2.5
- 0.2.4
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2.0
- v0.1.12
- v0.1.11
- 0.1.10
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- 0.1.0
- v0.0.7
- v0.0.6
- v0.0.5
- v0.0.4
- v0.0.3
- v0.0.2
- v0.0.1
This package is not auto-updated.
Last update: 2024-12-17 08:29:44 UTC
README
Allows you to use Twig seamlessly in Laravel 4.
NOTE: This is a fork of current TwigBridge by Robert Crowe. I maintained this because the current beta stage forces us to use dev branch of all repos. I do not want to use dev branch for all components, personally :)
Installation
Add gencer/twigbridge-laravel-4
as a requirement to composer.json:
{ "require": { "gencer/twigbridge-laravel-4": "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);
Configuration
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
Usage
You call the Twig template like you would any other view:
// Without the file extension View::make('i_am_twig', array(...))
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) }}
Extensions
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.
'extensions' => 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:
'extensions' => array( 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:
- Twig_Extension_Debug
- TwigBridge\Extension\Laravel\Auth
- TwigBridge\Extension\Laravel\Config
- TwigBridge\Extension\Laravel\Form
- TwigBridge\Extension\Laravel\Html
- TwigBridge\Extension\Laravel\Input
- TwigBridge\Extension\Laravel\Session
- TwigBridge\Extension\Laravel\String
- TwigBridge\Extension\Laravel\Translator
- TwigBridge\Extension\Laravel\Url
- TwigBridge\Extension\Loader\Facades
- TwigBridge\Extension\Loader\Filters
- TwigBridge\Extension\Loader\Functions
To enable '0.5.x' style Facades, enable the Legacy Facades extension:
- TwigBridge\Extension\Laravel\Legacy\Facades
FilterLoader and FunctionLoader
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.
FacadeLoader
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)
)
Functions/Filters/Variables
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:
- asset, action, url, route, secure_url, secure_asset
- auth_check, auth_guest, auth_user
- config_get, config_has
- form_* (All the Form::* methods, snake_cased)
- html_* (All the Html::* methods, snake_cased)
- input_get, input_old
- link_to, link_to_asset, link_to_route, link_to_action
- session_has, session_get, csrf_token
- str_* (All the Str::* methods, snake_cased)
- trans, trans_choice
- url_* (All the URL::* methods, snake_cased)
Filters:
- camel_case, snake_case, studly_case
- str_* (All the Str::* methods, snake_cased)
- trans, trans_choice
Global variables:
- app: the Illuminate\Foundation\Application object
- errors: The $errors MessageBag from the Validator (always available)
Artisan Commands
TwigBridge offers a command for CLI Interaction.
Empty the Twig cache:
$ php artisan twig:clean
Lint all Twig templates:
$ php artisan twig:lint