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.
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.
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.
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.
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.
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
Ah. Just needed this two liner:
$twig = $app->view->getInstance();
$twig->addFilter($filter);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With