OxCom / zf3-twig

ZendTwig is a module that integrates the Twig template engine with Laminas.
MIT License
19 stars 6 forks source link

JsonModel not work #9

Closed guilherme90 closed 7 years ago

guilherme90 commented 7 years ago

Yo.., allright?

In my Controller, will return JsonModel instance, but when call method, return:

ZendTwig\Renderer\TwigRenderer::render: Unable to render template "user/api/user-authentication/authenticate"; resolver could not resolve to a file

Gist: https://gist.github.com/guilherme90/33e97890ce0e8c4827a4146dc704f206

And yes, i set ViewJsonStrategy in view_manager -> strategies

How can I solve this? Thanks :)

guilherme90 commented 7 years ago

Solved! ;)

Here -> https://github.com/ZF-Commons/ZfcTwig/issues/83

OxCom commented 7 years ago

Hello,

If You take a look into class TwigStrategy, then You will find this code:

     /**
     * @param \Zend\View\ViewEvent $e
     *
     * @return \Zend\View\Renderer\RendererInterface|null
     */
    public function selectRender(ViewEvent $e)
    {
        if ($this->isForceRender()) {
            return $this->renderer;
        }
        $model = $e->getModel();
        if ($model instanceof TwigModel) {
            return $this->renderer;
        }
        return null;
    }

So, You can disable forcing of Twig Render in config like this:

'zend_twig' => [
        // ...
        /**
         * Force Your application to use TwigRender for ViewModel.
         * If false, then TwigStrategy will be applied only for TwigModel
         *
         * @note: In release v.1.5 this parameter will be set to false
         */
        'force_twig_strategy' => true, // <- change this to false
        // ...

After that all should be fine and You will be able to use mix of renders: php, json, twig and other in any order. As for ZF-Common, as I remember correctly - its depends on order of injection of strategies.

I hope than will help You.

guilherme90 commented 7 years ago

Thanks ;)

guilherme90 commented 7 years ago

Ahh, other question:

Various view_helpers not work too. e.g: headTitle, doctype, partial I need register this helpers to usage?

OxCom commented 7 years ago

By default some of them already registered, please, find in default module config:

'zend_twig' => [
        // ...
         /**
         * Developer can disable Zend View Helpers like docType, translate and e.t.c.
         */
        'invoke_zend_helpers' => true,
        // ..

So, they should be available. I have test for this, please, find here in twig template and here in test class. As for list of loaded helpers, please, see this file.

One of the docs for ZF3 modules there is recommendation to implement __invoke() method, so in this case we will have {{ som_view_helper() }}

guilherme90 commented 7 years ago

Yes, doctype is correct, but when I call this function, HTML5 doctype is not rendered, only xHTML 4 if I am not mistaken. The partial plugin is rendered, but the Twig tags are not interpreted.

I do not know if I'm doing something wrong, or lacking any configuration. If I do {% include 'my/template' %} works normally, and the Twig tags are interpreted.

OxCom commented 7 years ago

Step by step: 1) doctype should be set in view manager configuration or it can be directly set by __invoke(). By default this helper will render HTML4 doctype.

// ...
'view_manager' => [
    'doctype'  => \Zend\View\Helper\Doctype::HTML5,
// ...

2) \Zend\View\Helper\Partial is using PhpRender by default. So, You have to use Twig include

guilherme90 commented 7 years ago

Okay, thanks so much :)

kargpfen commented 6 years ago

Hello,

i have a Problem: When i change force_twig_strategy to false (JsonModel now working), my layout file will render with the PhpRenderer instead of TwigRenderer. I use zend_twig with 'force_standalone' => false. Can you help me please?

OxCom commented 6 years ago

@kargpfen When you did 'force standalone' => false, then you have to use TwigModel in your controllers. So, it should be something like this:

public function indexAction()
{
    return new \ZendTwig\View\TwigModel([
        'foo' => 'bar',
    ]);
}
kargpfen commented 6 years ago

@OxCom this is clear, but the parent view model (layout/layout) is a \Zend\View\Model\ViewModel instance.

thanks for your help.

OxCom commented 6 years ago

@kargpfen If it still not working, please, (re-)open ticket and provide way how to produce or some info about configuration. Thanks!

kargpfen commented 6 years ago

@OxCom i'm sorry. it was my mistake, the order in the modules list was wrong. :(