Use with Twig

Notice: This page documents version 1.0 of the Twig extension. See here for details on version 2.0.

Nice can easily support the Twig templating engine. All that's necessary is the installation of the Nice Twig extension and some simple configuration.

First, add the Nice Twig extension to your project. You can do this by updating your composer.json or running composer require at the command line.

  • Example composer.json:

        "require": {
            "nice/framework": "~1.0",
            "nice/twig": "~1.0"

    Then run composer update at the command line.

  • Using the composer command line tool, after Nice itself is installed:

    composer require nice/twig:~1.0

With the extension installed, all that's needed is some modifications to your front controller:


use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Nice\Application;
use Nice\Router\RouteCollector;
use Nice\Extension\TwigExtension;

require __DIR__ . '/../vendor/autoload.php';

$app = new Application();

// Register the Twig extension
$app->appendExtension(new TwigExtension(__DIR__ . '/../views'));

$app->set('routes', function (RouteCollector $r) {
    $r->map('/hello/{name}', null, function (Application $app, Request $request, $name) {
        // Use the Twig service to render templates
        $rendered = $app->get('twig')->render('index.html.twig', array(
            'name' => $name

        return new Response($rendered);

// Run the application

Once the TwigExtension is registered with your Nice application, the following services will be available:

  • twig is an instance of Twig_Environment. Use this service to render templates.

The parameter twig.template_dir is also made available. Its value will be whatever you've passed into the constructor of your TwigExtension instance.

Generating URLs

The Nice Twig extension includes integration with Nice's router, exposing the following useful functions:

  • path Generates a URL. It takes a name and optionally, an array of parameters.

    For example:

    <a href="{{ path('user_edit', { id: 1 }) }}">Edit User</a>
  • url Generates an absolute URL. It takes a name and optionally, an array of parameters.

  • asset Generates a URL to the given asset. It takes a relative path to the asset, from the webroot.

    For example:

    <link href="{{ asset('css/styles.css') }}" rel="stylesheet">

Powered by Codex 1.1.0