odan / slim4-skeleton

A Slim 4 Skeleton
https://odan.github.io/slim4-skeleton/
MIT License
446 stars 80 forks source link

Silent of absent Twig debug option #11

Closed f4t4l closed 4 years ago

f4t4l commented 4 years ago

Hi,

I am facing a weird behavior of twig while loading an array of items. Vars A, B, C, E, F load correctly but not D and G. When trying to add the twig debug extension in order to double check if there is a template issue or override, I realized that the integration of this extension is not functional.

Is there any compatibility issue between the slim4 skeleton and the twig-view v3?

I tried to enable the debug mode setting debug = true (also from the env.php). Tried also to add the DebugExtension in the container.php.

I am stuck ((

odan commented 4 years ago

Could you please add a minimal example?

f4t4l commented 4 years ago

The first behavior that pushed me to dive into the debug topic: user.twig <p>{{ user.companyAddress }} {{ user.companyLocality }} {{ user.companyZip}} {{ user.companyCountry }}.</p>

displays the fields without the country, while a simple var_dump shows me that Canada is in the user array:

1311 Avenue Bernard Outremont QC H2V 1W1 .

When adding a {{ debug(user) }} in the twig, I got the following error:

500 Internal Server Error - Error details: [0] Unknown "debug" function. in /var/www/html/templates/account/profile.twig on line 26

I added the following within the container.php file: config/container.php $twig->addExtension(new DebugExtension()); Tried also a former slim3 syntax I have in another project $twig->addExtension(new \Twig\Extension\DebugExtension());

config/development.php

$settings['twig']['options']['debug'] = true;
$settings['twig']['debug'] = true;

The error disappeared, the address is printed the same but no dump neither on the screen nor in the logs.

Configuration: docker php7.3-fpm, nginx

odan commented 4 years ago

The DebugExtension works and provides the dump function, not the debug function.

The dump function will only output something if Twig runs in debug mode.

  1. Enable the Twig debug mode:
$settings['twig']['options']['debug'] = true;
  1. Add the extension:
$twig->addExtension(new \Twig\Extension\DebugExtension());
  1. Then dump a variable using the dump function;
{{ dump(user) }}

https://twig.symfony.com/doc/3.x/functions/dump.html

f4t4l commented 4 years ago

Oops yes I made it wrong in the example sorry.

500 Internal Server Error - Error details: [0] Unexpected token "name" of value " " ("end of print statement" expected). in /var/www/html/templates/user.twig on line 26

This is my complete Twig templates section within container.php

Twig::class => function (ContainerInterface $container) {
        $config = $container->get(Configuration::class);
        $settings = $config->getArray('twig');

        $options = $settings['options'];
        $options['cache'] = $options['cache_enabled'] ? $options['cache_path'] : false;

        $twig = Twig::create($settings['paths'], $options);

        $loader = $twig->getLoader();
        if ($loader instanceof FilesystemLoader) {
            $loader->addPath($config->getString('public'), 'public');
        }

        $twig->getEnvironment()->addGlobal("loggedin", $container->get(Session::class)->get('user'));

        $twig->addExtension(new \Twig\Extension\DebugExtension());

        // Add extensions
        $twig->addExtension(new TranslationExtension($container->get(Translator::class)));
        $twig->addExtension(new WebpackExtension(
            $config->getString('public') . '/assets/manifest.json',
            'assets/',
            'assets/'
        ));

        // Add the Twig extension only if we run the application from the command line / cron job,
        // but not when phpunit tests are running.
        if ((PHP_SAPI === 'cli' || PHP_SAPI === 'cgi-fcgi') && !defined('PHPUNIT_TEST_SUITE')) {
            $app = $container->get(App::class);
            $routeParser = $app->getRouteCollector()->getRouteParser();
            $uri = (new UriFactory())->createUri('http://localhost');

            $runtimeLoader = new TwigRuntimeLoader($routeParser, $uri);
            $twig->addRuntimeLoader($runtimeLoader);
            $twig->addExtension(new TwigExtension());
        }

        /** @var FlashBagInterface $flashbag */
        $flashbag = $container->get(Session::class)->getFlashBag();
        $environment = $twig->getEnvironment();
        $environment->addGlobal('flashbag', $flashbag);
        $environment->addFunction(new TwigFunction(
            'flash',
            function (string $key, $default = null) use ($flashbag) {
                return $flashbag->get($key, $default ?? [])[0] ?? null;
            }
        ));

        $twig->addExtension(new FormattorExtension($container->get(Translator::class)));

        return $twig;
    },

development.php:

<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

$settings['env'] = 'development';

$settings['twig']['options']['cache_enabled'] = false;
$settings['twig']['options']['debug'] = true;
$settings['translation']['cache_enabled'] = false;

// Database
$settings['db']['database'] = 'slim_skeleton_dev';

user.twig

<div class="text-center user-info">
    <p>{{ dump(user) }}</p>
    <p class="lu">{{ loggedin.firstName}}{{ loggedin.lastName }}</p>
    <p>{{ user.companyAddress }} {{ user.companyLocality }} {{ user.companyZip}} {{ user.companyCountry }}.</p>
  </div>

Where line 26 contains:

{{ dump(user) }}

And the php var_dump: object(App\Domain\User\Data\UserViewData)[303] public 'id' => int 1 public 'email' => string 'admin@example.com' (length=17) public 'companyAddress' => string '1311 Avenue Bernard' (length=19) public 'companyLocality' => string 'Outremont' (length=9) public 'companyZip' => string 'QC H2V 1W1' (length=10) public 'companyCountry' => string 'Canada' (length=6)

odan commented 4 years ago

Check that you don't have non-breaking space in code before }}

f4t4l commented 4 years ago

You are right, there is an issue with spaces... Sorry I didn't think about this simple thing. Used Visual Code with twig editing extension.

Thank @odan you very much for your help reactivity and work