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

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,
));

Related

How to add extension Add global in twig

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.

render template that is in the views directory but inside another directory

I'm having a hard time trying to make this little modification...
I want to render a file (login.twig) which is inside my views file but in another folder: /views/ajax_files/login.twig
do I have to do this everytime ?
require 'vendor/autoload.php';
$loader = new Twig_Loader_Filesystem('views/ajax_files');
$twig = new Twig_Environment($loader, array(
'cache' => 'cache',
'auto_reload' => true,
));
echo $twig->render('login.twig');
Because on my index.php, that is already being declared
which I'm trying to write DRY code
require 'vendor/autoload.php';
$loader = new Twig_Loader_Filesystem('views');
$twig = new Twig_Environment($loader, array(
'cache' => 'cache',
'auto_reload' => true,
));
echo $twig->render('index.twig');
You can also define multiple template directories directly in your index.php with
$loader = new Twig_Loader_Filesystem(array($templateDir1, $templateDir2));
Please also have a look at http://twig.sensiolabs.org/doc/api.html#loaders, you could also use a loader chain or simply add the additional path to your existent filesystem loader, with some code like
$loader = $twig->getLoader();
$loader->addPath('another/path/to/templates'); //or $loader->prependPath('...')
$twig->setLoader($loader);

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;
};

render dump() in created Twig_Environment

I try use debug extension in new Twig_Environment here is example
$twig = new \Twig_Environment(new \Twig_Loader_String());
$twig->addExtension( new \Twig_Extension_Debug());
$rendered = $twig->render(
"out : {{ dump(array) }}",
array("array" => ['aa','bb'])
);
die($rendered);
give my out:
why dump() not work ?
ok i found
$twig = new \Twig_Environment(new \Twig_Loader_String(),array(
'debug' => true,
));

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