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

Can't use JsonModel and TwigModel #13

Closed
Alfredao opened this issue Oct 25, 2019 · 9 comments
Closed

Can't use JsonModel and TwigModel #13

Alfredao opened this issue Oct 25, 2019 · 9 comments
Assignees
Labels

Comments

@Alfredao
Copy link

Hi

I'm having the same problem as #9, when returning a JsonModel instance, I got this fatal error instead of the json response.

 Fatal error: Uncaught Zend\View\Exception\RuntimeException: ZendTwig\Renderer\TwigRenderer::render: Unable to render template "xxx"; resolver could not resolve to a file in /xxx/vendor/oxcom/zend-twig/src/Renderer/TwigRenderer.php on line 185

If I set force_twig_strategy to false, the JsonModel returns the json correctly, but then TwigModel does not render the twig templates

There's something I can do to solve this problem? Thank you all

@Alfredao Alfredao changed the title Can't use JsonModel while using this module Can't use JsonModel and TwigModel Oct 25, 2019
@Alfredao
Copy link
Author

With this configuration

    'zend_twig'    => [
        'force_twig_strategy' => false,
    ],

If I return the json model like this

    public function indexAction()
    {
        return new JsonModel([
            'testing' => 'Hello World'
        ]);
    }

I get the result as expected. See below

image

But instead of JsonModel, if I return a TwigModel, the twig files does not render

    public function indexAction()
    {
        return new TwigModel([
            'testing' => 'Hello World'
        ]);
    }

Result
image

@OxCom
Copy link
Owner

OxCom commented Oct 26, 2019

@Alfredao could you provide your configuration for module here and order of the modules in your application config (this module should be in the end, because of a way how zf3 is loading dependencies).

@OxCom OxCom added the bug label Oct 26, 2019
@OxCom OxCom self-assigned this Oct 26, 2019
@Alfredao
Copy link
Author

Hi @OxCom

My module order is

return [
    'Zend\Mvc\I18n',
    'Zend\Hydrator',
    'Zend\Router',
    'Zend\Validator',
    'DoctrineModule',
    'DoctrineORMModule',
    'Ecommerce',
    'ZendTwig',
];

This configuration is in Ecommerce module config

    'zend_twig'    => [
        'force_twig_strategy' => false,
        'force_standalone'    => false,
        'invoke_zend_helpers' => true,
        'environment'         => [
        ],
        'loader_chain'        => [
            \ZendTwig\Loader\MapLoader::class,
            \ZendTwig\Loader\StackLoader::class,
        ],
        'extensions'          => [
            \Ecommerce\View\Twig\Extension\Slugify::class,
            \Ecommerce\View\Twig\Extension\Money::class,
        ],
        'helpers'             => [
            'configs' => [
                \Zend\Navigation\View\HelperConfig::class,
            ],
        ],
    ],

@OxCom
Copy link
Owner

OxCom commented Oct 30, 2019

@Alfredao
CNR: Just checked in empty zend-skeleton. With the same configuration.

Please check what type of the template do you have for this indexAction()? It should be index.twig not index.phtml. Please check example

run:

composer install
cd ./public
php -S localhost:8000

check:

curl -D- http://127.0.0.1:8000/application/json
curl -D- http://127.0.0.1:8000/application/twig
curl -D- http://127.0.0.1:8000/application/index

@Alfredao
Copy link
Author

@OxCom Thank you for your time, this works ok.

But can we render the layout/layout configured in template_map with Twig as well?

'view_manager' => [
    'template_map' => [
        'layout/layout' => __DIR__ . '/../view/layout/layout.phtml', // this one with Twig
    ],
],

My plan is to give a template example for others developers create it's own layout, like Shopify does.
So its better for the template to be only twig files.

@OxCom
Copy link
Owner

OxCom commented Oct 31, 2019

@Alfredao change it to layout.twig and then use twig syntax. Example:

{{ docType() }}
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    {{ headTitle('Hello World')|raw }}

    {{
    headMeta()
        .appendName('viewport', 'width=device-width, initial-scale=1.0')
        .appendName('subject', 'Hello subject.')
        .appendName('description', 'World description.')
        .appendHttpEquiv('X-UA-Compatible', 'IE=edge')
    |raw
    }}

    {{
    headLink()
        .headLink({
            'rel' : 'shortcut icon',
            'type' : 'image/vnd.microsoft.icon',
            'href' : ServerUrl('/img/favicon.ico'),
        })
        .appendStylesheet(ServerUrl('/css/styles.min.css'))
    |raw
    }}
</head>
<body>
{% block content %}
    {{ content|raw }}
{% endblock content %}

{% block content_scripts %}
    {{ content_scripts|default('')|raw }}
{% endblock content_scripts %}
</body>
</html>

@Alfredao
Copy link
Author

Sorry for not giving enough information, but that is my problem. If I change layout.phtml extension to twig it can't be rendered. Look

image

I could not find the problem, but I think ZendView is rendering the layout.twig file as PHP and this module is rendering only the content files

OxCom pushed a commit that referenced this issue Dec 4, 2019
@OxCom
Copy link
Owner

OxCom commented Dec 5, 2019

@Alfredao please update to the latest version with a fix.

@Alfredao
Copy link
Author

Alfredao commented Dec 5, 2019

Hi @OxCom, tested yesterday! Thank you so much

@Alfredao Alfredao closed this as completed Dec 5, 2019
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

2 participants