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

Twig Trying to Render Json Model #83

Open
tom-martin7 opened this issue Jan 31, 2014 · 5 comments
Open

Twig Trying to Render Json Model #83

tom-martin7 opened this issue Jan 31, 2014 · 5 comments

Comments

@tom-martin7
Copy link

Hi,

I have just came across an issue where the twig strategy is trying to render a Json Model even though this should be possible.

The fix should be as follows: ZfcTwig\View\TwigStrategy::selectRenderer

This function should have the following before you run the can render function.

if ($e->getModel() instanceof \Zend\View\Model\JsonModel)
{
    return false;
}

Many Thanks,
Tom

@lku
Copy link

lku commented Feb 3, 2014

It's trying to render it only if model's template exists, which seems logical to me.

@kusmierz
Copy link

How to prevent it? I would like to have one action for twig and json respond. How could I do that?

@spiffyjr
Copy link
Contributor

This is just internal to how ZF2 works. If you return a JsonModel then the JsonViewStrategy is used. If you return a ViewModel then the default Twig/PhpRendererStrategy is used.

You need to have your strategies registered in the right order. JsonViewStrategy should be set in 'view_manager' => 'strategies' earlier than Twig is loaded.

@kusmierz
Copy link

I'm not sure. Try ie. https://github.com/zendframework/ZendSkeletonApplication, install it, but in config/application.config.php file put ZfcTwig at the beginning of the modules (just before Application module).

@skl
Copy link

skl commented Sep 16, 2015

I was also affected by this and in my case changing the order of the modules broke my app. To get around this I registered a ModuleEvent listener that prioritises ViewJsonStrategy over ZfcTwigViewStrategy in order to satisfy the ZF2 requirement that @spiffyjr mentions above.

Here's a gist for it.

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

No branches or pull requests

5 participants