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,
));
Related
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,
));
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.
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;
};
In my database I have escapde each string with addslashes http://php.net/manual/fr/function.addslashes.php
Now I want to "unescape" my data with stripslashes to display them in a twig view http://php.net/manual/fr/function.stripslashes.php
I'm not using a framework. Only a PHP MVC architecture. Twig was installed with composer and works fine.
Here is how I tried to implement my new filter in TWIG :
require './vendor/autoload.php';
/**
* TWIG
*/
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem('vue');
$twig = new Twig_Environment($loader, array('debug' => true));
$twig->addExtension(new Twig_Extension_Debug());
$filter = new Twig_SimpleFilter('strips', function ($string) {
return stripslashes($string);
});
$twig->addFilter($filter);
How I try to use :
{{ realisation.getName | strips }}
And the answer :
Fatal error: Uncaught exception 'Twig_Error_Syntax' with message 'The filter "strips" does not exist in "allReal.twig"
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).