How to add extension Add global in twig - php

When I try add the Extension Add global in twig
$view = new \Slim\Views\Twig($settings['template_path'], [
'debug' => $settings['debug'],
'cache' => $settings['cache_path']
]);
// Add extensions
$view->addExtension(new \Slim\Views\TwigExtension(
$c['router'],
$c['request']->getUri()
));
$view->addExtension(new \Core\TwigFunction());
$view->addExtension(new Twig_Extension_Debug());
$view = new Twig_Environment();;
$view->addGlobal('session', $_SESSION);
I receive:
Call to undefined method Twig_Environment::offsetSet()

In the line
$view = new Twig_Environment();
You are overriding the $view variable. Delete ing that line should solve the problem as slim/twig-view sets up the Twig environment for you.

Related

Container->get('route') method in slim framework

What does Container->get('route') method returns and why
It is used in twig extension with slim
The Slim Twig extension requires the router (not the route) instance for the custom template functions like path_for() and base_url().
Example:
// Register Twig View helper
$container['view'] = function ($c) {
$view = new \Slim\Views\Twig('path/to/templates', [
'cache' => 'path/to/cache'
]);
// Instantiate and add Slim specific extension
$router = $c->get('router');
$uri = \Slim\Http\Uri::createFromEnvironment(new \Slim\Http\Environment($_SERVER));
$view->addExtension(new \Slim\Views\TwigExtension($router, $uri));
return $view;
};
Please read the slimphp/Twig-View documentation.

Slim + Twig - how to turn off Twig cache during development?

This is the view which is twig that I inject it in the container in Slim:
// Views and Templates
// https://www.slimframework.com/docs/features/templates.html
$container['view'] = function ($container) {
$settings = $container->get('settings');
$loader = new Twig_Loader_Filesystem('templates');
$twig = new Twig_Environment($loader, array(
'cache' => 'cache',
));
// Add twig extension.
$twig->addExtension(new \Twig_Extensions_Extension_Array());
return $twig;
};
With this setting, Twig always read the template from the cache. Is there any way I can turn off the cache during the development?
change "cache" to false.
Like this
$twig = new Twig_Environment($loader, array(
'cache' => false,
));

How do I enable debuggin of Twig when used with Slim?

Per http://twig.sensiolabs.org/doc/api.html#using-extensions:
When creating a new Twig_Environment instance, you can pass an array
of options as the constructor second argument:
$twig = new Twig_Environment($loader, array('debug' => true));
How would this be implemented when used with Slim? My attempt is below, however, {{ dump(foo) }} results in an Unknown "dump" function in "forms.html" at line 35. error. I have tried adding the script on the Twig website, however, I don't know what to use for $loader and where to insert $twig.
$container['view'] = function ($c) {
$view = new \Slim\Views\Twig('../application/templates', [
//'cache' => 'path/to/cache' // See auto_reload option
'debug' => true,
'strict_variables'=> true
]);
$view->addExtension(new \Slim\Views\TwigExtension(
$c['router'],
$c['request']->getUri()
));
return $view;
};

Volt.php files not auto-updating

While using the volt template the files ending with .volt would auto generate a volt.php file and if any changes were made to the volt file, the volt.php file would not be updated unless deleted manually for it to generate a new volt.php file. Is there a way for it to auto-update when changes were made?
Thanks in advance.
There wasn't. I don't think this has changed. You can however set compileAlways flag while in development, or set it depending on your app environment variable, if you use one. Read this for more details.
$di->set('view', function(){
$view = new \Phalcon\Mvc\View();
$view->setViewsDir('../app/views/');
$view->registerEngines(array(
'.volt' => function($view, $di) {
$volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
$volt->setOptions(array(
'compiledPath' => '../app/compiled/',
'stat' => true,
'compileAlways' => true
));
return $volt;
}
));
return $view;
});

Symfony 2 Twig form functions not available

I am creating a new Twig Environment object inside a custom class, that class is being rendered from an existing twig file. I am trying to render a form in my twig file which is being rendered from my custom class, however in this new Twig_Environment object form functions are not available, i have tried adding existing form extension from symfony's own twig object to my new twig object, that is not working either.
$path = 'some/directory'; // just simplifying here
$loader = new \Twig_Loader_Filesystem( $path );
$twig = new \Twig_Environment($loader, array(
'cache' => __DIR__.'/../../../../../../app/cache/',
));
$tmpl = $twig->loadTemplate('EmailUs.html.twig');
$twig->addExtension( new \Symfony\Bridge\Twig\Extension\FormExtension( $this->pageObj->getContainer()->get('twig.form.renderer') ) );
$response = new Response();
$response->setContent($tmpl);
return $response;
The error i get is
"The function "form_start" does not exist in EmailUs.html.twig at line
8"
I was using symfony's own twig object to render the response but that was giving me the same error. Can you help pls? I am using Symfony 2.3.4
Form functions are available if i render a normal controller, they dont work fine if i create a custom twig object.
I have solved it this way:
$loader = $pageFunctions->getContainer()->get('twig.loader');
$loader->addPath( $path );
$twig = new \Twig_Environment($loader, array(
'cache' => __DIR__.'/../../../../../../app/cache/myTwig',
));
foreach( $this->twig->getExtensions() as $ext ) {
$twig->addExtension( $ext );
}
$tmpl = $twig->loadTemplate('EmailUs.html.twig');
$rendered = $tmpl->display( array('control' => $this,
'functions' => $pageFunctions,
'params' => $params,
'email_form'=>$form->createView() ) );
return $rendered;
Here's my working code (executed inside a controller action):
$path = __DIR__.'/../Resources/views/'; /* twig loader path */
$loader = new \Twig_Loader_Filesystem($path);
$twig = new \Twig_Environment($loader);
$twig->addExtension( new \Symfony\Bridge\Twig\Extension\FormExtension($this->get('twig.form.renderer')));
$tmpl = $twig->loadTemplate('test.html.twig');
return new Response($tmpl->render(array()));
I've mainly moved the addExtension call before the loadTemplate one (otherwise extensions would have been already initialized).

Categories