mtymek/ZfTwig

ZfTwig with ZF2 beta 3

krmarien opened this issue · 15 comments

Hello,

I'm building a website with ZF2 beta 3 and ZfTwig (branch zf-beta-3). I configured everything like in the readme, except this:

$view = $locator->get('view');

is

$view = $locator->get('ZfTwig\TwigRenderer');

But now I get the following error:

Warning: array_merge() [function.array-merge]: Argument #2 is not an array in /Users/kristofmarien/Sites/Groenveld/vendor/ZfTwig/vendor/Twig/lib/Twig/Environment.php on line 96

Does ZfTwig not work yet with ZF2 beta 3 or what did I forgot?

Thanks

Hi,
I have a prototype beta-3 compatible module here: https://github.com/mtymek/ZfTwig/tree/zf-beta-3. However, it is not finished yet, I'm still not sure what is the best way of configuring consuming modules yet (asked questhin here, but no answer yet: http://zend-framework-community.634137.n4.nabble.com/Configuring-Zend-View-to-use-different-engines-than-PhpRenderer-tp4464049p4464049.html).
Anyway, please wait one or two days - I'll do some refactoring in my prototype and publish updated version with new readme then.

Ok, I hope with you they will help you soon

Module is now updated to work nicely with beta 3. I still need to clean up the code and work a bit more on docs though...

Thank you for your work!

Hello,

I'm trying to implement ZfTwig, but I get the following error:

Fatal error: Maximum function nesting level of '100' reached, aborting! in /vendor/ZendFramework/library/Zend/Stdlib/PriorityQueue.php on line 146

This is my config file:

'Zend\View\Resolver\AggregateResolver' => array(
    'injections' => array(
        'Zend\View\Resolver\TemplateMapResolver',
        'Zend\View\Resolver\TemplatePathStack',
    ),
),
'Zend\View\Resolver\TemplateMapResolver' => array(
    'parameters' => array(
        'map'  => array(
            'layout' => __DIR__ . '/../view/layout/layout.twig',
        ),
    ),
),
'Zend\View\Resolver\TemplatePathStack' => array(
    'parameters' => array(
        'paths'  => array(
            'application' => __DIR__ . '/../view',
        ),
        'defaultSuffix' => 'twig'
    ),
),
'ZfTwig\TwigRenderer' => array(
    'parameters' => array(
        'resolver' => 'Zend\View\Resolver\AggregateResolver',
    ),
),

That's very weird. I did some tests and discovered that AggregateResolver is injected with itself (!) which causes endless loop when trying to resolve view script file.
I didn't notice that initially, because I had the following in my config:

'Zend\View\Renderer\PhpRenderer' => array(
            'parameters' => array(
                'resolver' => 'Zend\View\Resolver\AggregateResolver',
            ),
),

Please add this and let me know if it solved your problem. I'll try to figure it out and see if it is my fault, or something wrong in Zend\View.

Ok, the endless loop is gone, but now I have the following error:

 Fatal error: Call to a member function load() on a non-object in /vendor/ZfTwig/src/ZfTwig/TwigEnvironment.php on line 44

Do you know what is the cause of this error?

Yes - wrong ZfTwig configuration :-)
Please pull latest version.

Thanks, the twig files are now displayed :)

Ok, I thought everything worked...
Because only the template defined in the config file is displayed.

'Zend\View\Resolver\TemplateMapResolver' => array(
    'parameters' => array(
        'map'  => array(
            'layout' => __DIR__ . '/../layouts/layout.twig',
        ),
    ),
),

But the views aren't, but if I make these corrupt (write invalid twig or remove them) I get an error it's invalid.

The only thing ZfTwig must do for me is parse the views. Because I start these with:

{% extends layouts/layout.twig %}

If I change the file in the code above to something invalid, twig doesn't throw an exception. So the code in the view is parsed, but never displayed.

What would cause this problem?

Please use '"Zend-style" layouts for now, ie. remove "extends" directive and use following in your layout.twig:

{{ content }}

(this will have the same meaning as <?php echo $this->content ?> in PhpRenderer)

In order to use "extends" directive, you'd need to disable Zend layouts via DI configuration (please refer to Zend\View docs if you want to do that).

I'll should proably add this to the FAQ...

I'm sorry, I prefer the not "Zend-style" layout :)

In the controller I can disable the layout (ViewModel->setTerminal(true)), but I haven't found the implemention by the DI.
In the previous version of ZfTwig we could add paths to the the view by this function:

$view->getEnvironment()->getLoader()->addPath();

Now it must be done by the DI, but if I add these they can't be found:

'Zend\View\Resolver\TemplatePathStack' => array(
    'parameters' => array(
        'paths'  => array(
            'layout'      => __DIR__ . '/../layouts',
            'application' => __DIR__ . '/../views',
        ),
        'defaultSuffix' => 'twig'
    ),
),

I need them for the include tag of twig.

Thanks,

I found a way how to achive this.

  1. Pull latest changes

  2. Inject template resolver into ZfTwig\Template loader class:

    'ZfTwig\TemplateLoader' => array(
            'parameters' => array(
                'resolver' => 'Zend\View\Resolver\AggregateResolver'
            )
    ),
    
  3. Disable default layout from within action (I didn't found a way to do it via Di...):

    public function indexAction()
    {
        $viewModel = new ViewModel();
        $viewModel->setTerminal(true);
        return $viewModel;
    }
    
  4. Use your layout :-)

    {% extends 'layout/layout' %}
    {% block content %}
     ....
    {% endblock %}
    

Ok, the default pages work now,
But the problem now is error pages. Because since beta3 there isn't an ErrorController anymore I cannot disable the layout there. Is there a way to solve this problem?

Now I use the default config for error pages:

// View script rendered in case of 404 exception
'Zend\Mvc\View\RouteNotFoundStrategy' => array(
    'parameters' => array(
        'displayNotFoundReason' => true,
        'displayExceptions'     => true,
        'notFoundTemplate'      => 'error/404',
    ),
),
// View script rendered in case of other exceptions
'Zend\Mvc\View\ExceptionStrategy' => array(
    'parameters' => array(
        'displayExceptions' => true,
        'exceptionTemplate' => 'error/index',
    ),
),

Thanks!

I would also love to get the error pages working with twig. The idea is to use twig renderer for not found and exception strategies and disable wrapping it all in layout.