TYPO3 Twig Extension

This TYPO3 extension allows you to use the fabulous Twig template engine within your TYPO3 project. You can use Twig templates in your Extbase controllers or in your TypoScript.

build license packagist TYPO3

Use one of the most used and easy to
learn templating engines for PHP.

Easy to learn

Twig is easy to learn and allows to write templates without hassle.

Use in other projects

In comparison to Fluid, Twig is also used in other PHP frameworks like Symfony.

Extendable

Twig is extendable using functions, filters and more writing simple PHP classes.

Get started

This extension can only be installed using composer.


composer require cvc/typo3-twig

Moving from Fluid to Twig

Twig provides a easy syntax to create templates. It not only used in a wide range of PHP frameworks, but also known outside the PHP community.

If you want to move from Fluid to Twig, you can learn about Twig and its build in extensions on Twig’s official website.

Rendering of Twig templates in TYPO3 works nearly exactly the same way as rendering Fluid templates. See the examples below how to render the templates within Extbase or within TypoScript.

Render Twig templates in TypoScript

The TWIGTEMPLATE Object Renderer allows you to render Twig template files.

Variables can be set using Content Objects. This allows you to use TEXT, COA or others.

You can pass a list of template directories to the engine. This allows to override templates from other extensions.

page.10 = TWIGTEMPLATE
page.10 {
    templateName = example.html.twig
    variables {
        foo = TEXT
        foo.value = Bar!
    }
    templateRootPaths {
        10 = EXT:twig/Resources/Private/TwigTemplates
    }
}

Use in your Extbase controller

<?php
// my_extension/Classes/Controller/MyFantasticalController.php

class MyFantasticalController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{
    // setting the default view object class to TwigView will enable the Twig templates
    protected $defaultViewObjectName = \Cvc\Typo3\CvcTwig\Extbase\Mvc\View\TwigView::class;

    public function someAwesomeAction()
    {
        // assign the variables as usual
        $this->view->assign('foo', 'BAZ!');
    }
}

Let the TwigView automatically detect the right template for your action. As with using Fluid you simply have to configure the template root paths.

Have a look into the Documentation for more information.

Helpers at your hand

The extension ships with functions and filters that will assist you in creating your templates.

Create a link to another page

<a href="{{ t3_uri_page(123) }}">Link to another page</a>

Parse HTML from rich text editor fields

{{ data.bodytext | t3_html }}

Dump variables on the screen

{{ dump(variable) }}

Use Twig

This extension can be easily installed using composer. Read the "Get Started" guide to get more information about using Twig with TYPO3.

Contribute

Found a bug? Missing a feature? We are happy about all types of contribution. Create an issue or a merge request on the GitHub project.

Work with us

We are a web development agency based in Essen, Germany. Contact us if you would like to work with us.