bittyphp/view-twig

A Twig template view for Bitty.


Keywords
bitty, php, twig, view
License
MIT

Documentation

Twig View

Build Status Codacy Badge PHPStan Enabled Mutation Score Total Downloads License

A Twig template view for Bitty.

Installation

It's best to install using Composer.

$ composer require bittyphp/view-twig

Setup

You can use any valid Twig Environment options.

Basic Usage

<?php

require(dirname(__DIR__).'/vendor/autoload.php');

use Bitty\Application;
use Bitty\View\Twig;

$app = new Application();

$app->getContainer()->set('view', function () {
    return new Twig(dirname(__DIR__).'/templates/', $options);
});

$app->get('/', function () {
    return $this->get('view')->renderResponse('index.twig', ['name' => 'Joe Schmoe']);
});

$app->run();

Multiple Template Paths

If you have templates split across multiple directories, you can pass in an array with the paths to load from.

<?php

use Bitty\View\Twig;

$twig = new Twig(
    [
        'templates/',
        'views/',
    ]
);

$twig->render('foo.twig');
// Looks for the following templates until it finds one:
// 'templates/foo.twig'
// 'views/foo.twig'

Namespaced Templates

You can also add namespaces to the template directories. If you have multiple templates with the same name but in different directories, this makes it really easy to reference a particular template.

<?php

use Bitty\View\Twig;

$twig = new Twig(
    [
        'admin' => 'templates/admin/',
        'public' => 'templates/public/',
    ]
);

$twig->render('@admin/index.twig');

Adding Extensions

One of the great things about Twig is that you can easily extend it to add you own custom functionality. This view would not be complete without allowing access to that ability.

<?php

use Bitty\View\Twig;

$twig = new Twig(...);

/** @var Twig_ExtensionInterface */
$extension = ...;

$twig->addExtension($extension);

Advanced

If you need to do any advanced customization, you can access the Twig environment and loader directly at any time.

<?php

use Bitty\View\Twig;

$twig = new Twig(...);

/** @var Twig_Environment */
$environment = $twig->getEnvironment();

/** @var Twig_LoaderInterface */
$loader = $twig->getLoader();