Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a custom filter to my Twig templates inside of Slim?

Tags:

php

twig

slim

Using the example from http://twig.sensiolabs.org/doc/advanced.html#creating-an-extension: within my main Slim file that creates the view:

$filter = new Twig_SimpleFilter( 'stripslashes', function ( $string ) { 
    return stripslashes( $string );
});

$loader = new \Twig_Loader_String();
$twig = new Twig_Environment($loader);
$twig->addFilter($filter);

$app->view($twig);

$app->view()->setData( array(
    'nav' => $nav,
    'sidenav' => $sidenav,
));

Results in: Call to undefined method Twig_Environment::appendData().

Tried in various ways such as this:

$app->view(new \Slim\Views\Twig());
$app->view->parserExtensions = array(
    new \Slim\Views\TwigExtension(),
);

$app->view->addFilter($filter);

but I'm just not understanding how it's supposed to work.

like image 988
meder omuraliev Avatar asked Dec 31 '14 01:12

meder omuraliev


People also ask

How do you use a Twig filter?

Variables in Twig can be modified with help of twig filters. Filters are simply separated from variables by a pipe symbol ( | ). For applying twig filter we only need to apply the ( | ) followed by filter name. Twig comes with many filters built into it, and Drupal has a variety of filters native to it.

What are filters in Twig?

Filters in Twig can be used to modify variables. Filters are separated from the variable by a pipe symbol. They may have optional arguments in parentheses. Multiple filters can be chained. The output of one filter is applied to the next.

What is raw in Twig?

raw. By default, everything in Twig gets escaped when automatic escaping is enabled. If you don't want to escape a variable you'll have to explicitly mark it as safe which you can do by using the raw filter. This only works if the raw filter is the last filter that is applied to the filter.

Is Twig a template engine?

Twig is a modern template engine for PHPSecure: Twig has a sandbox mode to evaluate untrusted template code. This allows Twig to be used as a template language for applications where users may modify the template design.


2 Answers

For Slim 3, things have changed. It can be done in one line:

$view->getEnvironment()->addFilter($filter);

But that isn't particularly useful without context, so here is a full sample, based on the example provided at the Slim Framework Website: http://www.slimframework.com/docs/features/templates.html

This code demonstrates adding a filter to encode text with rot13

<?php
// Create app
$app = new \Slim\App();

// Get container
$container = $app->getContainer();

// Register component on container
$container['view'] = function ($container) {
   $view = new \Slim\Views\Twig('path/to/templates', [
       'cache' => 'path/to/cache'
    ]);
    $view->addExtension(new \Slim\Views\TwigExtension(
        $container['router'],
        $container['request']->getUri()
    ));

    $filter = new Twig_SimpleFilter('rot13', function ($string) {
        return str_rot13($string);
    });

    $view->getEnvironment()->addFilter($filter);

    return $view;
};

// Render Twig template in route
$app->get('/rot13/{text}', function ($request, $response, $args) {
    return $this->view->render($response, 'rot13.html', [
        'name' => $args['text']
    ]);
})->setName('rot13');

// Run app
$app->run();

And the html file rot13.html contains:

{{text|rot13}}

Point your browser at yourservername/rot13/pineapple and you should see

cvarnccyr
like image 90
Phippsy Avatar answered Sep 18 '22 10:09

Phippsy


Ah. Just needed this two liner:

$twig = $app->view->getInstance();
$twig->addFilter($filter);
like image 27
meder omuraliev Avatar answered Sep 19 '22 10:09

meder omuraliev