Support the ongoing development of Laravel.io →
Views Packages

Laravel 4 TwigBridge: https://github.com/barryvdh/laravel-twigbridge

This packages adds Twig as a Laravel Template Engine:

  • Use .twig files just like Blade/PHP Templates
  • Supports creator/composer events
  • Easily add helpers/filters ({{ url('/') }} or {{ 'someThing' | snake_case }})
  • Can call Facades ({{ MyModel.to('/') }})
  • Can be integrated with Assetic (https://github.com/barryvdh/laravel-assetic)
  • Default extensions for easier use.

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": "dev-master"

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'); or Twig::render('index.twig', $data) 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:

  • asset, action, url, route, secure_url, secure_asset
  • link_to, link_to_asset, link_to_route, link_to_action
  • auth_check, auth_guest, auth_user
  • config_get, config_has
  • session_has, session_get, csrf_token
  • trans, trans_choice
  • form_* (All the Form::* methods, snake_cased)
  • html_* (All the Html::* methods, snake_cased)
  • str_* (All the Str::* methods, snake_cased)

Filters:

  • camel_case, snake_case, studly_case
  • str_* (All the Str::* methods, snake_cased)

Global variables:

  • app: the Illuminate\Foundation\Application object
  • errors: The $errors MessageBag from the Validator (always available)

Read more on https://github.com/barryvdh/laravel-twigbridge

Last updated 3 years ago.
0

Thank you very much sir.

Last updated 3 years ago.
0

This package has been deprecated in favor of http://github.com/rcrowe/TwigBridge version 0.6.0 Syntax and features are all available in this new version, just have to replace the config file.

This is the result of combining our work to make a more robust/tested/extendable twig bridge :)

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

barryvdh barryvdh Joined 11 Dec 2013

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.