0

I have implemented a simple template system using Oscommerce 2.3.4. So that I can run multiple shops, with the potential to change the content or look of a shop on a 'site' or 'template' basis.

The core OSC files (module files, classes etc.) still reside in directories off the app root, but the templates, language files and stylesheets reside in 'templates' and 'sites' directories. Each 'template' or 'site' directory structure is the same as the standard structure of the OSC app. If a file is present in site / template folders it will be used instead.

There is a default template directory for all the default template and language files. The precedence for selecting which file to use goes like this:

  1. Look to see if there is a site specific file (sites/***/...).
  2. Else, look to see if there is a template specific file. (templates/***/...)
  3. Else, Use the file in the default template directory. (templates/default/...)

I am considering to use the set_include_path function to list each of these directories, mentioned above, to take advantage of a behaviour of the include_path as documented on php.net:

PHP considers each entry in the include path separately when looking for files to include. It will check the first path, and if it doesn't find it, check the next path, until it either locates the included file or returns with a warning or an error.

This seems almost ideal for my situation, however I couldn't find whether this was considered 'good' practice.

I was interested if there are any pitfalls of using this such as, a performance hit. Considering that a lot of includes will now look in 3 directories before finally finding the default template in the 4th location. (Although is this any worse than using if(file_exists()) multiple times which is the alternative.)

Also it will be necessary to set these include paths in the top of the app as the paths use variables for current site and template from the app. So i can't set the include path in the php.ini

Any help or advice much appreciated

Majid Abbasi
  • 1,531
  • 3
  • 12
  • 22
dading84
  • 1,210
  • 12
  • 18
  • On big issue is that you can't catch the exception which occurs when the file is not found without catching `Exception` which is a anti-pattern. – max Nov 14 '17 at 12:33
  • @max I send all my errors to a log file, and display on screen in the development environment. Would I not get the error message if no file is found and included? – dading84 Nov 14 '17 at 12:45
  • Right, but you can't catch the exception and provide a better log message. I would use the template loader from Symfony2 or Laravel instead of reinventing the wheel. – max Nov 14 '17 at 13:47
  • @sam thanks for the advice. The use of templates in my site is v simple, it simply separates original files into logic code and display (templates), so that visual display can be replaced for sites / templates (theme). Both are just PHP files. I didn't mention in my question, but I avoid making any changes to the core files of OSCommerce. I think you would need considerable changes to the core files to integrate a 3rd party template engine. One of the main benefits of proceeding as I describe above is I would not need to make any changes to code in the core OSC files. – dading84 Nov 14 '17 at 14:19

0 Answers0