Home

Awesome

Laravel 4 TwigBridge

Note: This package is deprecated in favor of https://github.com/rcrowe/TwigBridge. Please try the 0.6.x release, which should be very similar and provide the same functionality. Further progress will take place there.

This packages adds Twig as a Laravel Template Engine:

See http://twig.sensiolabs.org/ for more info about Twig Templating

Install

Require this package in your composer.json and run composer update (or run composer require barryvdh/laravel-twigbridge:dev-master directly):

"barryvdh/laravel-twigbridge": "0.3.x"

After updating composer, add the ServiceProvider to the providers array in app/config/app.php

'Barryvdh\TwigBridge\ServiceProvider',

You can add the Twig Facade to have easy access to Twig_Environment, ie. Twig::render('template.twig').

'Twig' => 'Barryvdh\TwigBridge\Twig',

Usage

After install, you can just use View::make('index'); The .twig extension should be omitted in the View::make() call, just like Blade files. Within your Twig files, you can reference them with or without .twig. You can also use view composers/creators, just like in Blade templates.

View::composer('profile', function($view)
{
	$view->with('count', User::count());
});

Extensions

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:

Example Template Syntax

In a Blade template, if you had a route to edit a task in a Task/Todo application, you would use the following syntax to link to a route.

{{ link_to_route('tasks.edit', 'Edit', $task->id, array('class' => 'btn btn-primary')) }}

In a Twig template you would do the same thing using the following syntax. Notice the task object drops the dollar sign ($) and instead of an arrow (->) you use a period ('.'). Also, you convert the array to a Python/Javascript dictionary type syntax.

{{ link_to_route('tasks.edit', 'Edit', task.id, {'class': 'btn btn-primary'}) }}

Commands

2 Artisan commands are included:

Configure

To publish a configuration file, you can run the following command:

$ php artisan config:publish barryvdh/laravel-twigbridge

Change your config to choose what helpers/filters you want to use, and what Facades to register. You can also pass in a callback or array to define options. You can also use an instance of Twig_SimpleFunction or Twig_SimpleFilter. Besides facades, you can also add your Models.

'functions' => array(
	'simple_function',
	'class_function' => 'MyClass@method',
	'other_function' => array(
		'is_safe' => array('html')
	),
	'call_me' => array(
		'is_safe' => array('html'),
		'callback' => function($value){ 
				return phone($value);
			}
	)
),

'filters' => array(
	'filter_this' => function($value){
			return doSomething($value);
		}
),

'facades' => array(
	'Auth', 
	'MyModel'
)

Extend

The Twig_Environment is available as 'twig' in the App Container, so you can access it via app('twig') or App::make('twig'). The ChainLoader is 'twig.loader', the array templates are in 'twig.templates'. You can also use the Twig Facade to access the Twig_Environment functions directly.

//Using the App container
$twig = app('twig');
$twig->addFunction(new Twig_SimpleFunction(..));

$loader = App::make('twig.loader');
$loader->addLoader($myLoader);

//Using the Facade
Twig::addGlobal('key', $value);
Twig::addFunction(new Twig_SimpleFunction(..));
Twig::getLoader()->addLoader($myLoader);

//Adding templates to the array loader
App::extend('twig.templates', function($templates){
        $templates['hello'] = 'Hello World!';
        return $templates;
    });
echo Twig::render('hello'); //Hello World!