2017 © Pedro Peláez
 

library twigbridge

Adds the power of Twig to Laravel

image

rcrowe/twigbridge

Adds the power of Twig to Laravel

  • Tuesday, July 3, 2018
  • by rcrowe
  • Repository
  • 50 Watchers
  • 717 Stars
  • 860,036 Installations
  • PHP
  • 47 Dependents
  • 4 Suggesters
  • 145 Forks
  • 85 Open issues
  • 59 Versions
  • 7 % Grown

The README.md

Allows you to use Twig seamlessly in Laravel., (*1)

Latest Stable Version Total Downloads test License, (*2)

Requirements

TwigBridge >= 0.13 supports Twig 3. If you need Twig 1/2 support, use the 0.12 versions., (*3)

Installation

Require this package with Composer, (*4)

composer require rcrowe/twigbridge

Quick Start

Laravel automatically registers the Service Provider. Use Artisan to publish the twig config file:, (*5)

php artisan vendor:publish --provider="TwigBridge\ServiceProvider"

At this point you can now begin using twig like you would any other view, (*6)

//app/Http/routes.php
//twig template resources/views/hello.twig
Route::get('/', function () {
    return View::make('hello');
});

You can create the twig files in resources/views with the .twig file extension., (*7)

resources/views/hello.twig

Configuration

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 bottom and add:, (*8)

'TwigBridge\ServiceProvider',

You can add the TwigBridge Facade, to have easier access to the TwigBridge (or Twig\Environment)., (*9)

'Twig' => 'TwigBridge\Facade\Twig',
Twig::addExtension('TwigBridge\Extension\Loader\Functions');
Twig::render('mytemplate', $data);

TwigBridge's configuration file can be extended in your ConfigServiceProvider, under the twigbridge key. You can find the default configuration file at vendor/rcrowe/twigbridge/config., (*10)

You should use Artisan to copy the default configuration file from the /vendor directory to /config/twigbridge.php with the following command:, (*11)

php artisan vendor:publish --provider="TwigBridge\ServiceProvider"

If you make changes to the /config/twigbridge.php file you will most likely have to run the twig:clean Artisan command for the changes to take effect., (*12)

Installation on Lumen

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 + Service Provider in bootstrap/app.php:, (*13)

$app->configure('twigbridge');
$app->register('TwigBridge\ServiceProvider');

Usage

You call the Twig template like you would any other view:, (*14)

// Without the file extension
View::make('i_am_twig', [...])

TwigBridge also supports views in other packages:, (*15)

View::make('pagination::simple')

The above rules continue when extending another Twig template:, (*16)

{% extends "parent" %}
{% extends "pagination::parent" %}

You can call functions with parameters:, (*17)

{{ 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., (*18)

{{ 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/twigbridge.php a list of extensions for Twig to load., (*19)

'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:, (*20)

'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:, (*21)

  • Twig\Extension\DebugExtension
  • TwigBridge\Extension\Laravel\Auth
  • TwigBridge\Extension\Laravel\Config
  • TwigBridge\Extension\Laravel\Dump
  • TwigBridge\Extension\Laravel\Form
  • TwigBridge\Extension\Laravel\Gate
  • 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, (*22)

FilterLoader and FunctionLoader

These loader extensions exposes Laravel helpers as both Twig functions and filters., (*23)

Check out the config/twigbridge.php file to see a list of defined function / filters. You can also add your own., (*24)

FacadeLoader

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., (*25)

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)), (*26)

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., (*27)

Functions: * asset, action, url, route, secure_url, secure_asset * auth_check, auth_guest, auth_user * can * config_get, config_has * dump * form_ (All the Form:: methods, snake_cased) * html_ (All the Html:: methods, snake_cased) * input_get, input_old, input_has * link_to, link_to_asset, link_to_route, link_to_action * session_has, session_get, csrf_token, csrf_field, method_field * str_ (All the Str:: methods, snake_cased) * trans, trans_choice * url_ (All the URL:: methods, snake_cased), (*28)

Filters: * camel_case, snake_case, studly_case * str_ (All the Str:: methods, snake_cased) * trans, trans_choice, (*29)

Global variables: * app: the Illuminate\Foundation\Application object * errors: The $errors MessageBag from the Validator (always available), (*30)

Artisan Commands

TwigBridge offers a command for CLI Interaction., (*31)

Empty the Twig cache:, (*32)

$ php artisan twig:clean

Lint all Twig templates:, (*33)

$ php artisan twig:lint

The Versions

17/06 2014

v0.5.5

0.5.5.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

26/03 2014

0.5.4

0.5.4.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

31/01 2014

0.5.3

0.5.3.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

22/09 2013

0.5.2

0.5.2.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

27/08 2013

0.5.1

0.5.1.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

01/08 2013

0.5.0

0.5.0.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

31/07 2013

0.4.1

0.4.1.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

06/06 2013

0.4.0

0.4.0.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

07/05 2013

0.3.1

0.3.1.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

30/04 2013

0.3.0

0.3.0.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

27/04 2013

0.2.6

0.2.6.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

26/04 2013

0.2.5

0.2.5.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

21/03 2013

0.2.4

0.2.4.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

16/03 2013

0.2.3

0.2.3.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

15/03 2013

0.2.2

0.2.2.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

15/03 2013

0.2.1

0.2.1.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

15/03 2013

0.2.0

0.2.0.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

21/02 2013

v0.1.12

0.1.12.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

18/02 2013

v0.1.11

0.1.11.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

18/02 2013

0.1.10

0.1.10.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

06/02 2013

v0.1.9

0.1.9.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

05/02 2013

v0.1.8

0.1.8.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

24/01 2013

v0.1.7

0.1.7.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

23/01 2013

v0.1.6

0.1.6.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

16/01 2013

v0.1.5

0.1.5.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

16/01 2013

v0.1.4

0.1.4.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

12/01 2013

v0.1.3

0.1.3.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

10/01 2013

v0.1.2

0.1.2.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

10/01 2013

v0.1.1

0.1.1.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel twig

10/01 2013

0.1.0

0.1.0.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

BSD

The Requires

 

The Development Requires

laravel twig

22/12 2012

v0.0.7

0.0.7.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

BSD

The Requires

 

laravel twig

06/12 2012

v0.0.6

0.0.6.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

BSD

The Requires

 

laravel twig

06/12 2012

v0.0.5

0.0.5.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

BSD

The Requires

 

laravel twig

06/12 2012

v0.0.4

0.0.4.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

BSD

The Requires

 

laravel twig

30/11 2012

v0.0.3

0.0.3.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

BSD

The Requires

 

laravel twig

27/11 2012

v0.0.2

0.0.2.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

BSD

The Requires

 

laravel twig

27/11 2012

v0.0.1

0.0.1.0

Adds the power of Twig to Illuminate / Laravel 4

  Sources   Download

BSD

The Requires

 

laravel twig