Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonModel not work #9

Closed
guilherme90 opened this issue Sep 17, 2017 · 13 comments
Closed

JsonModel not work #9

guilherme90 opened this issue Sep 17, 2017 · 13 comments
Assignees
Labels

Comments

@guilherme90
Copy link

guilherme90 commented Sep 17, 2017

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
Copy link
Author

Solved! ;)

Here -> ZF-Commons/ZfcTwig#83

@OxCom
Copy link
Owner

OxCom commented Sep 17, 2017

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
Copy link
Author

Thanks ;)

@guilherme90
Copy link
Author

Ahh, other question:

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

@OxCom
Copy link
Owner

OxCom commented Sep 17, 2017

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
Copy link
Author

guilherme90 commented Sep 17, 2017

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
Copy link
Owner

OxCom commented Sep 18, 2017

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,
// ...
  1. \Zend\View\Helper\Partial is using PhpRender by default. So, You have to use Twig include

@guilherme90
Copy link
Author

Okay, thanks so much :)

@OxCom OxCom closed this as completed Sep 23, 2017
@kargpfen
Copy link

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
Copy link
Owner

OxCom commented Nov 28, 2017

@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
Copy link

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

thanks for your help.

@OxCom
Copy link
Owner

OxCom commented Nov 28, 2017

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

@kargpfen
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants