MMv4 KB
 
Using PHP includes in Templates
  Last Edited - 07/17/2012 4:15pm PDT
  Category Path - Developer's Guide > Code Samples
 
Depending on your store design, you might find that you have specific content that you want to have appear on every page of your storefront. An example of this could be a navigation menu, footer or third-party traffic analysis tool.

To replicate this content on every storefront page, one approach could be to add the content to every single template used by your store. However, if you later want to change something in this repeated content, you'll have to edit every single template. That's a lot of maintenance for every little change that you want to make.

A way to avoid this maintenance nightmare is to place the repeated content in its own file, then use PHP to include that file on every page. That way, even though the content is repeated on every storefront page, its coming from one central file. Changes to the shared content can be done by editing just the one included file.

For example, you can use the following line of code to include the file "example.php", located in the "httpdocs" directory.

code
<?php include("example.php"); ?>

When a page with this code is opened, the contents of "example.php" will be added wherever the include() statement was placed in the template.

tip
Note that the SEO-friendly URLs used by the shopping cart software can sometimes make determining the path to the included file a bit tricky.  You can avoid this problem by saving your replicated content in a custom QuickCode™ Tag. 

Then, instead of using PHP include() statement to insert the content into each template, use the custom QuickCode™ tag instead. Replacing the PHP include() with a custom QuickCode Tag, such as {CUSTOM-123}, precludes the need to specify a file path.

More articles on the subject of custom QuickCode Tags are available.
Powered by ModularKB