Open CaptainJojo opened 10 years ago
what is the exact error?
Same error here, and I also can't understand how it should work. Did you test it? The twig environment is trying to load a file, since there is no string loader registered by default. Since there is no template file, twig can't load it and fails with:
Unable to find template "{{ apply_filter_0122b4c2c01ee1c698ecc309d2b8eb5a|upper }}"
Maybe you got newer Twig versions where it isn't possible anymore. In tests the base stuff is covered https://github.com/marcj/twig-apply_filter-bundle/blob/master/Tests/BaseTest.php
Possible, maybe it's Symfony related. Can't tell you. The workaround is pretty easy. Just backup the current loader on the environment using getLoader() and set the new one to a Twig_String_Loader (Twig_Loader_String?) instance. Now you're able to load strings as templates. After loading, you can reset the loader to the previous one using setLoader().
Twig_String_Loader is marked as deprecated. So using that would probably be a bad idea. I did manage however to use $env->createTemplate($template) instead of loadTemplate at which point the script kept executing. Downside probably would be that a new cache template is created time after time.
Hi,
I propose to you a solution using the Twig_loader_FileSystem. This solution is working on symfony 2.7 with the advantage to not generate a lot of supplementary cache. What do you think about this?
/**
* Apply twig filter of the environment by using theirs names (as string).
*
* Exemple: {{ value|apply_filter("upper") }}
*
* @param \Twig_Environment $env
* @param array $context
* @param $value
* @param $filters
*
* @return string
*/
public function applyFilter(\Twig_Environment $env, $context = array(), $value, $filters)
{
$fs = new Filesystem();
//set the needed path
$template_dir_path = $env->getCache().'/apply_filter';
$template_file_name = $filters.'.html.twig';
$template_path = $template_dir_path.'/'.$template_file_name;
//create dir for templates in twig cache
if(!$fs->exists($template_dir_path))
$fs-mkdir($template_dir_path);
if(!$fs->exists($template_path))
{
//write the new template if first call
$template = sprintf('{{ value|%s }}', $filters);
file_put_contents($template_path,$template);
}
//store the old loader (not sure that is necessary)
$old_loader = $env->getLoader();
//use file loader
$loader = new \Twig_Loader_Filesystem($template_dir_path);
$env->setLoader($loader);
$rendered = $env->render($template_file_name,array("value" => $value));
//reload the previous loader
$env->setLoader($old_loader);
return $rendered;
}
Same error with sf 2.7
Same error with sf 2.8 Not working, not testing!
I highly doubt this bundle is maintained any longer.
I use your Bundle but is not functionnal, I have variable with node.value and node.filter in my twig. I use node.value|apply_filter(node.filter) I have an error, because not find template why ?