OxCom / zf3-twig

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

Can't use JsonModel and TwigModel #13

Closed Alfredao closed 4 years ago

Alfredao commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

@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).

Alfredao commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

@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 commented 5 years ago

@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 commented 5 years ago

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 commented 4 years ago

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

Alfredao commented 4 years ago

Hi @OxCom, tested yesterday! Thank you so much