Closed fabiocarneiro closed 10 years ago
In order to get view helpers working I had to duplicate the view helper manager from ZF2. This is a limitation of ZF2 currently and should be resolved in ZF3. If you have any view helpers that are registered after ZfcTwig clones the ZF2 view helper manager you will have to register them with Twig's view helper manager manually.
See: https://github.com/ZF-Commons/ZfcTwig/blob/1.2rc2/config/module.config.php#L59
EDIT You will want to use the 1.2rc2 branch.
I want to use twig templates only in some part of my project. I already have my .phtml views and i dont want to convert them.
I'm storing my views in database for i18n purposes, but i need them to contain urls and maybe view helpers like "5 random products" and "services for this location", etc... storing php code didnt seem a good idea, so i was thinking about writing some parser, until someone told me to use Twig. I wrote that code in my controller (maybe not the best place, but nvm), that would get the template from this $content variable, which is a doctrine entity, parse it in twig and write back. The following code is currently working in dev-master, but the custom view helpers are the problem. Isnt there any way to inject my view helpers before the view helper manager duplication?
$enviroment = $this->getServiceLocator()->get('ZfcTwigEnvironment');
$loader = clone $enviroment->getLoader();
try {
$enviroment->getLoader()->addLoader(new Twig_Loader_String());
$content->setContent($enviroment->render($content->getContent()));
$enviroment->setLoader($loader);
} catch(\Twig_Error_Runtime $e){
$enviroment->setLoader($loader);
throw new \Exception("Error rendering layout");
}
return new ViewModel(array('content' => $content));
There is no ZfcTwigEnvironment in 1.2rc2, so it didnt work. I tried to replace it for Twig_Enviroment, but it didnt work too.
EDIT: I also tried to setup the view helpers, but i'm not sure you understood, the view helpers are returning empty string in normal .phtml files, not in twig templates. if i had to setup the view helpers only for twig templates it would be ok, but that way i'll have to write all my view helpers, even from vendor modules in zfctwig config. I tried to use that helper_manager index but even writing some view helpers here, i wasnt able to render them in phtml files neither twig templates.
The architecture of ZF2 makes using phtml and twig files together very difficult. The issues generally arise when dealing with the "partial" view helper but there are other's that cause issues. If you have to mix phtml and twig together the best thing to try is to load all your modules before your load ZfcTwig in your application.config.php. This won't handle all use-cases but it might work for you.
Unfortunately, I can't really recommend much more. It's a weak point in the ZF2 view layer that can't really be fixed without breaking backwards compatibility.
The zfctwig is already my last module in application.config.php.
The problem is that when i enable zfctwig, i'm not able to use the view helpers in any of the two template types.
Make a note of any BC features that are missing so I can try to make sure the rc branch is BC.
Its is important to make sure your viewHelpers use the correct view renderer. Setting the view render will make all view helpers to work properly in both twig templates and phtml templates. I had a problem all my view helpers renderers where replaced by the zfctwig view renderer and they stopped working.
$viewHelper->setViewRenderer($helperPluginManager->getServiceLocator()->get('ViewRenderer'));
Closing, reopen if it's still an issue.
Hi! I had a Zend\View\AbstractHelper that was working ok in 1.1.5, switching to 1.2 kind of castrated it (blank output, no errors). I've searched through docs to find the solution, saw reference to a config key under 'zfctwig' called 'helper_manager' -- can't seem to find the right configuration.
Presently, the helper is loaded with getViewHelperConfig() (as is standard); what am I missing to connect those dots?
Thanks for your help
@Saeven I did answered your question in StackOverflow: http://stackoverflow.com/questions/21435146/how-do-i-register-zf2-helpers-with-zfc-twig-1-2
I have no idea why, but after enabling ZfcTwig in my project, all view helpers start to return string ''. Disabling it makes everything work again.
Something i noticed, is that the default zf2 view helpers are still working. For instance, url helper and menu helper are still generating menus and urls, but custom helpers return string ''. Maybe there is something overriding the custom helper logic, causing it to fail.