Post by oc2020 » Sun Jun 14, 2020 1:53 am

Another weird thing I also saw some other user mentioned is, even if I use theme editor to edit and save my twig file, sometimes the change isn't saved when I navigate somewhere else and come back to the same twig file. Any ideas why this thing happens?

New member

Posts

Joined
Wed May 13, 2020 5:11 am

Post by letxobnav » Sun Jun 14, 2020 3:15 pm

Normally the twig rendering engine compares the dates of the common/header.twig and its processed cache file to see whether it should update the cache with your changes.
I don't think twig compares cache file datetime unless you use set autoreload or debug which both are actually for template development.

Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by straightlight » Sun Jun 14, 2020 6:08 pm

See this FAQ about TWIG changes: viewtopic.php?f=134&t=215776#p718325

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by letxobnav » Sun Jun 14, 2020 6:28 pm

I know but that simply deletes the theme cache files, the question was whether twig cache files would expire after a set period or twig checks whether the cache files were stale.

Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by JNeuhoff » Sun Jun 14, 2020 7:29 pm

Part of the solution would be to use the 'auto_reload', e.g. something like this:

system/library/template/twig.php

Code: Select all

	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, 'auto_reload'=>true);

		if ($cache) {
			$config['cache'] = DIR_CACHE;
		}

		$this->twig = new \Twig_Environment($loader, $config);
		
		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();	
		}	
	}	
catalog/controller/event/theme.php

Code: Select all

	public function index(&$route, &$args, &$template) {
		$this->log->write("ControllerEventTheme::index: route='$route' priority=1000");
		// If there is a template file we render
		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, 'auto_reload'=>true);
			
			if ($this->config->get('template_cache')) {
				$config['cache'] = DIR_CACHE;
			}
			
			// initialize Twig environment
			$twig = new \Twig_Environment($loader, $config);
				
			return $twig->createTemplate($template)->render($args);
		}
	}	
According to https://symfony.com/doc/current/referen ... uto-reload :
If true, whenever a template is rendered, Symfony checks first if its source code has changed since it was compiled. If it has changed, the template is compiled again automatically.

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by letxobnav » Sun Jun 14, 2020 8:47 pm

true but I understand that that is a development setting so I am not sure about the performance impact.

The following options are available:

debug boolean

When set to true, the generated templates have a __toString() method that you can use to display the generated nodes (default to false).

charset string (defaults to utf-8)

The charset used by the templates.

cache string or false

An absolute path where to store the compiled templates, or false to disable caching (which is the default).

auto_reload boolean

When developing with Twig, it’s useful to recompile the template whenever the source code changes. If you don’t provide a value for the auto_reload option, it will be determined automatically based on the debug value.

strict_variables boolean

If set to false, Twig will silently ignore invalid variables (variables and or attributes/methods that do not exist) and replace them with a null value. When set to true, Twig throws an exception instead (default to false).

autoescape string

Sets the default auto-escaping strategy (name, html, js, css, url, html_attr, or a PHP callback that takes the template “filename” and returns the escaping strategy to use – the callback cannot be a function name to avoid collision with built-in escaping strategies); set it to false to disable auto-escaping. The name escaping strategy determines the escaping strategy to use for a template based on the template filename extension (this strategy does not incur any overhead at runtime as auto-escaping is done at compilation time.)

optimizations integer

A flag that indicates which optimizations to apply (default to -1 – all optimizations are enabled; set it to 0 to disable).

Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by JNeuhoff » Sun Jun 14, 2020 10:31 pm

There are no observable performance reductions when using 'auto_reload'. In fact, it's finally added in the latest OpenCart master branch.

Having said that, IMHO it was a bad idea to use Twig in the first place, PHP already is a template language anyway.

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by straightlight » Mon Jun 15, 2020 7:54 am

JNeuhoff wrote:
Sun Jun 14, 2020 10:31 pm
There are no observable performance reductions when using 'auto_reload'. In fact, it's finally added in the latest OpenCart master branch.

Having said that, IMHO it was a bad idea to use Twig in the first place, PHP already is a template language anyway.
Correct. I am correctly in the wonders as to know if it is simply feasible to run a composer update with older versions of the TWIG engine towards a new TWIG engine or should it require modifications to the files mentioned above.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by oc2020 » Mon Jun 15, 2020 11:27 am

JNeuhoff wrote:
Sun Jun 14, 2020 10:31 pm
There are no observable performance reductions when using 'auto_reload'. In fact, it's finally added in the latest OpenCart master branch.

Having said that, IMHO it was a bad idea to use Twig in the first place, PHP already is a template language anyway.
Hi JNeuhoff, is this safe to use your codes in the previous reply to replace my current ones? I'm a little frustrated about how this twig cache slow down my tests. (I'm using 3.0.3, btw)

New member

Posts

Joined
Wed May 13, 2020 5:11 am

Post by letxobnav » Mon Jun 15, 2020 12:27 pm

I just change:

Code: Select all

            'auto_reload' => null,
to

Code: Select all

            'auto_reload' => true,
in system/library/template/twig/environment.php

Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by Melmoth » Wed Jun 17, 2020 12:02 am

''auto_reload' => true' didn't work for me. It's weird because no matter the piece of code I change in .twig files it doesn't affect the webpage, but if I delete the same twig files, the webpage actually crashes... ??? so those are the correct files but the code changes doesn't affect... (I've cleared cache, from directories and from inside the dashborad in OC)

I'm really lost with this, thank you for your ideas

Newbie

Posts

Joined
Tue Jun 16, 2020 11:22 pm

Post by xxvirusxx » Wed Jun 17, 2020 12:57 am

Melmoth wrote:
Wed Jun 17, 2020 12:02 am
It's weird because no matter the piece of code I change in .twig files it doesn't affect the webpage, but if I delete the same twig files
Edited with what?
When you make changes don't forget first to : Refresh Ocmod modifcations and clear VQMOD cache if you use then clear Theme cache, SASS cache from Dashboard right top gear.

Upgrade Service | OC 2.3.0.2 PHP 8 | My Custom OC 3.0.3.8 | Buy me a beer


User avatar
Expert Member

Posts

Joined
Tue Jul 17, 2012 10:35 pm
Location - România

Post by Melmoth » Wed Jun 17, 2020 4:40 pm

Thank you for your fast anwser mate.
xxvirusxx wrote:
Wed Jun 17, 2020 12:57 am
Edited with what?
Good question because I used to use OC native template editor to make small changes, but since I've got this problem, OC template editor doesn't load any of the twig files. (Maybe that's a clue for finding a solution) so I edit the files using FTP with Filezilla and my code editors.
xxvirusxx wrote:
Wed Jun 17, 2020 12:57 am
When you make changes don't forget first to : Refresh Ocmod modifcations and clear VQMOD cache if you use then clear Theme cache, SASS cache from Dashboard right top gear.
Yes, I've already tried that but problem persists.

Newbie

Posts

Joined
Tue Jun 16, 2020 11:22 pm

Post by oc2020 » Thu Jun 18, 2020 10:21 pm

letxobnav wrote:
Mon Jun 15, 2020 12:27 pm
I just change:

Code: Select all

            'auto_reload' => null,
to

Code: Select all

            'auto_reload' => true,
in system/library/template/twig/environment.php
This doesn't work for me either. This twig cache issue is the biggest confusion for me so far, and here is what I'm experiencing:

Scenario A:
1. use theme editor to change some twig files from Admin Theme Editor
2. save it
3. clear cache from admin panel
4. check my website and see the changes happens
5. check the same file next day from theme editor and the file goes back to the older one before I changed anything

Scenario B:
1. use an external editor to edit the twig file
2. save it and use FTP to upload
3. clear cache from admin panel
4. check my website and don't see any changes
5. to make this work I have to copy/paste the same code through Theme Editor but then I go back to Scenario A

New member

Posts

Joined
Wed May 13, 2020 5:11 am

Post by letxobnav » Thu Jun 18, 2020 10:32 pm

scenario A
check the same file next day from theme editor and the file goes back to the older one before I changed anything
Since the theme editor stores the source in the database.
I would say that you or your host is restoring or losing database content at some interval.

Scenario B
to make this work I have to copy/paste the same code through Theme Editor
Since you have a version of the file stored in the database (theme table) which overides the core and any modification file, file changes there will have no effect.

So either use the theme editor (which you should never do as it is crap) or stick to using OCMOD for all changes.

Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by oc2020 » Thu Jun 18, 2020 11:45 pm

letxobnav wrote:
Thu Jun 18, 2020 10:32 pm
scenario A
check the same file next day from theme editor and the file goes back to the older one before I changed anything
Since the theme editor stores the source in the database.
I would say that you or your host is restoring or losing database content at some interval.

Scenario B
to make this work I have to copy/paste the same code through Theme Editor
Since you have a version of the file stored in the database (theme table) which overides the core and any modification file, file changes there will have no effect.

So either use the theme editor (which you should never do as it is crap) or stick to using OCMOD for all changes.
this is good to know! thanks for the explanation!

So if I use theme editor to edit a certain file, it doesn't change the .twig file directly but just saves a changed copy in database?

And if I decided to stop using Theme Editor from now on, what I should do to make it work for an external editor/FTP approach (my scenario B)?

New member

Posts

Joined
Wed May 13, 2020 5:11 am

Post by letxobnav » Fri Jun 19, 2020 8:50 am

And if I decided to stop using Theme Editor from now on, what I should do to make it work for an external editor/FTP approach (my scenario B)?
remove the version from the theme editor.
If you need to keep those changes, save the source and put it in an ocmod or copy it over the core.
In the end, keep your theme table empty.

Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by straightlight » Fri Jun 19, 2020 7:57 pm

letxobnav wrote:
Fri Jun 19, 2020 8:50 am
And if I decided to stop using Theme Editor from now on, what I should do to make it work for an external editor/FTP approach (my scenario B)?
remove the version from the theme editor.
If you need to keep those changes, save the source and put it in an ocmod or copy it over the core.
In the end, keep your theme table empty.
We don't copy over the core!

Create new event files would be an alternative solution.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by letxobnav » Fri Jun 19, 2020 8:14 pm

We don't copy over the core!
There is no We.

Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by straightlight » Fri Jun 19, 2020 9:04 pm

letxobnav wrote:
Fri Jun 19, 2020 8:14 pm
We don't copy over the core!
There is no We.
'We' as an indirect term. Regardless, we don't provide solutions to override core files on the forum.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON
Who is online

Users browsing this forum: Google [Bot], moreduff and 269 guests