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

Cant use dump() function in twig files [oc3.0.2.0] #5933

Closed
jaynarayan89 opened this issue Sep 3, 2017 · 10 comments
Closed

Cant use dump() function in twig files [oc3.0.2.0] #5933

jaynarayan89 opened this issue Sep 3, 2017 · 10 comments

Comments

@jaynarayan89
Copy link

jaynarayan89 commented Sep 3, 2017

I am trying to find which variables are available to use in the template using dump() function of twig but it gives error of undefined function.

Then I search twig documentation and find out that it is disabled by default and we have to enable it but I think there is not any config option to turned on twig template debug in opencart 3.0.2.0.

when i dig the source code ,I found that twig is being initiated in catalog\controller\event\theme.php controller. I changed index method (just for testing)to activate debug mode but it didn't work.

if ($template) {
			// include and register Twig auto-loader
			include_once(DIR_SYSTEM . 'library/template/Twig/Autoloader.php');
			
			Twig_Autoloader::register();
					
			// specify where to look for templates
			$loader = new \Twig_Loader_Filesystem(DIR_TEMPLATE);		
			
			$config = array('autoescape' => false);
			
			if ($this->config->get('template_cache')) {
				$config['cache'] = DIR_CACHE;
			}

			**// create template_debug seting in config file
                       if ($this->config->get('template_debug')) {
				$config['cache'] = true;
			}**
			// initialize Twig environment
			$twig = new \Twig_Environment($loader, $config);
			**$twig->addExtension(new Twig_Extension_Debug());**	
			return $twig->createTemplate($template)->render($args);
		}

I think there should be a option to enable or disable twig debuging.

@lipedeitos
Copy link

Hi @jaynarayan89 i got this working changing the main file in /system/library/template/twig.php.
Add the 'debug' => true to the $config array.
And finally add the $this->twig->addExtension(new \Twig_Extension_Debug()); right after the $this->twig = new \Twig_Environment($loader, $config);.

public function render($template, $cache = false) {
    // specify where to look for templates
    $loader = new \Twig_Loader_Filesystem(DIR_TEMPLATE);
    // initialize Twig environment
    $config = array('autoescape' => false, 'debug' => true);
    if ($cache) {
        $config['cache'] = DIR_CACHE;
    }
    $this->twig = new \Twig_Environment($loader, $config);
    $this->twig->addExtension(new \Twig_Extension_Debug());
	
    try {
        // load template
        $template = $this->twig->loadTemplate($template . '.twig');

        return $template->render($this->data);
    } catch (Exception $e) {
        trigger_error('Error: Could not load template ' . $template . '!');
        exit();	
    }	
}	

Cheers!

@jaynarayan89
Copy link
Author

@lipedeitos Thanks.
But this should be builtin feature. It would be inappropriate to change the core files.

@ghost
Copy link

ghost commented Nov 22, 2017

OpenCart bullshit man what did you expect :/

@danielkerr
Copy link
Member

And another childish comment.

@RajeshThakur
Copy link

You can activate dump() function for twig environment by following these steps in opencart 3.0 :

  1. Go to system/library/template/Twig/Environment.php
  2. Here in constructor function you can find $options array. Set 'debug' option to 'true'.
  3. Now we need to include debug extension, so
    add $this->addExtension(new Twig_Extension_Debug()); below.
  4. That's all we need to do. Now test if its working for you and update here.

@lSanik
Copy link

lSanik commented Jun 3, 2018

RajeshThakur: - Thank you MAN!)
danielkerr: - Bad! Fail - "-F". Sitdown pleas! XD

@stom66
Copy link

stom66 commented Jun 18, 2018

Sadly neither of these methods appear to work in OpenCart 3.0.2.0 under PHP 5.6.33, 7.0.27 or 7.1.18.

Is there an alternative way to dump template variables for debugging?

Edit: Whoops! My fault. @RajeshThakur's solution works perfectly and I had a typo in my template. Be sure to use the correct syntax: {{ dump() }}

@shyaminayesh
Copy link

@RajeshThakur 's solution working fine in following environment.

OpenCart Version : 3.0.2.0
PHP Version : 7.0.23

@hemantramphul
Copy link

Thanks @RajeshThakur

@jpasosa
Copy link

jpasosa commented Jan 22, 2019

Thanks @RajeshThakur !! I test in OC 3.0.2 and perfect!

after use {{ dump()}} i use the next code, to know all keys in the view:
<ol>{% for key, value in _context %}<li>{{ key }}</li> {% endfor %} </ol>

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

9 participants