justin's zen cart schtick

The Zen Cart Execution Process

Zen Cart, like most open-source code, is a largly undocumented. Luckily for us, it also has a habit of mixing in much of it's program/application logic with it's actual HTML design, so it's not only undocumented, it's also hard to browse the HTML code. To top it off, it also has a dynamic template loader, which means that the program logic and HTML templates could be in any one of four different directories. Also undocumented, and of dubious usefullness.

The result is that it's not only difficult to browse through the program logic and HTML code, but you often don't even know which files are going to get loaded.

This document is an attempt to sort through the overarching structure of how Zen Cart tends to work.

index.php steps:

Here's where all of action begins.
  1. it loads 'includes/application_top.php'
  2. if $main_page isn't set, it gets set to 'index'
  3. if the directory 'includes/modules/pages/$main_page' doesn't exist, $main_page gets set to 'index'
  4. it loads all of the files starting with 'header_php' in 'includes/modules/pages/$main_page'
  5. now it loads 'html_header.php' from the first first place it finds it from the following directories
    1. includes/templates/mytemplate/$main_page/
    2. includes/templates/template_default/$main_page/
    3. includes/templates/mytemplate/common/
    4. includes/templates/template_default/common/
  6. now it does the same thing for finding and loading 'main_template_vars.php'
  7. now it does the same thing for finding and loading 'tpl_main_page.php'
  8. Finally it displays the '</html>' tag. Why display it here when the '<html>' tag is displayed somewhere else? Who knows.

application_top.php steps:

This file will load in all of general definitions used site-wide, initialize some site-wide classes ($template, $db) and do other general stuff.

html_header.php steps:

The default html_header.php file will simply create all of the HTML up to the </head> tag It loads 'includes/modules/meta_tags.php', which uses a bloated switch statement to figure out what the site's title, meta-keywords, and meta-description should be. It tries to find the css directory, and loads in it all stylesheets that begin with 'style' and end with '.css' It does the same thing for the jscript directory, and tries to load all files that begin with 'jscript_' and end with '.js'

main_template_vars.php steps:

it simply sets $body_code to the first of:
  1. includes/modules/pages/$main_page/main_template_vars.php
  2. includes/templates/mytemplate/$main_page/tpl_{$main_page}_default.php
  3. includes/templates/template_default/$main_page/tpl_{$main_page}_default.php
  4. includes/templates/mytemplate/templates/tpl_{$main_page}_default.php
  5. includes/templates/template_default/templates/tpl_{$main_page}_default.php
that's it.

tpl_main_page.php steps:

this file lays out the basic page.
  1. <body>
  2. loads the 'includes/modules/header.php' file
  3. if the left column is enabled, it loads 'includes/modules/column_left.php'
  4. it loads the file set in $body_code
  5. if the right column is enabled, it loads 'includes/modules/column_right.php'
  6. loads 'includes/modules/footer.php'
  7. </body>

includes/modules/pages/$main_page/main_template_vars.php steps:

this is where the meat of the actual content if loaded and displayed. it typically does all of the program logic first, sets all of the necessary variables, and then loads the template file. As usual, the template file is loaded from the first of the following:
  1. includes/templates/mytemplate/$main_page/
  2. includes/templates/template_default/$main_page/
  3. includes/templates/mytemplate/templates/
  4. includes/templates/template_default/templates/
The standard is that the template file will be named 'tpl_SOMENAME.php'. Note that it doesn't end with '_default.php'. The undocumented naming convention is that files that end with '_default.php' are files that get loaded with it can't find the 'includes/modules/pages/$main_page/main_template_vars.php' file.

tpl_{$main_page}_default.php steps:

If it doesn't find the above file and it instead finds a tpl_*_default.php file, then it means that all of the program logic and variables will be mixed up with the actual displayed HTML code. In Zen-Cart's defense, this generally means that there's not a lot of program logic to be had here, hardly any really, but it's still damn annoying to be mixing the program logic with design. No sir, I don't like it one bit.

Future Updates

Eventually this document will also cover the process by which the program loads in the language files, such as "english.php" and the content files like "privacy.php" and "conditions.php." The Zen Cart creators actually created these files with the idea that they would be a part of your custom template, which is a clear violation of the content/design seperation.

This document will also cover sideboxes (which requires a whole nother custom template directory). As an interesting sidenote, when you switch to a new template, all of the sideboxes get turned off. The level of user-unfriendliness of Zen Cart is amazing to me sometimes.

lifefeed@gmail.com    »«    more zencart goodness    »«    home