Using PHP in your templates.

 
avatar

In the process of creating this website, one of the challenges I faced was altering the templates of various applications that this site utilizes so that they would look and behave like the rest of our site. To be honest, it was quite a challenge at times because we have our blogs (powered by Wordpress), our community forum (powered by phpBB), our online store (powered by CubeCart), and also a subscription management system which is a very heavily customized version of aMember Pro (which uses the popular Smarty template system). And wouldn't you know it? They all use different template systems!

For the most part changing templates for all of them was a relatively straight-forward process. However, the navigation menu on the left side of this site is dynamically generated from our content management system (which was custom built by me so there is no template problem there). In order to maintain a consistent presentation I was faced with the challenge of including the PHP scripts to generate that menu in the templates. For those of you that have worked with templates, this seems like a huge challenge since the purpose of template systems is to remove the design files from the application scripts. I was intimidated, and for a couple of the templates (Cubecart, Smarty) I was convinced that I wasn't going to be able to do it. But in the end, I figured it all out, and I wanted to share my experiences for others that are working with templates.

Wordpress Blogs

The Wordpress blogs were by far the easiest templates to work with since they are written in straight PHP. Simply include the needed code, make some formatting adjustments, and you are done. If you are a PHP developer, you should have no problem at all adding custom PHP scripts to the Wordpress templates. Look for the header.php file to change stuff at the top of the page, and the footer.php file to change stuff at the bottom of the page. The sidebar.php template file changes the look of the sidebar menu, and then there are other pages that let you get at all the content code. Again, this is a very easy template system to work with, relative to others that I have seen.

phpBB Community Forum

While an open source and pretty damn good forum application, I have to say that phpBB has a pretty poor templating system. There are a LOT of files to change. There is an overallheader.tpl and an overallfooter.tpl file that control (obviously) the header and the footer. Everything else is in a separate file and is done in a table-based layout that is a real pain to change. However, it's just a matter of putting in the time and getting it done, and the results are worth it.

To get PHP to work in the phpBB forum software templates, I had to actually get a mod for the application. It is something called "Extreme Styles" and it (among other things that I don't use it for) will allow you to use direct PHP code in the templates, which is nice. Once installed, adding custom PHP functionality is as easy and straight-forward as it is with WordPress. So while there was still a lot of time put into the forum templates, with the help of that "Extreme Styles" mod it wasn't terribly difficult.

CubeCart Templates

One of the reasons that we chose CubeCart was that I really like the template system in CubeCart. It is very well organized and very easy to work with. However, to add custom PHP functionality to the templates was probably the most difficult in this one. There wasn't any way to inject the code that I wanted into the footer file (which is where the navigation PHP code needed to go) with the way that CubeCart gathers templates together. For this one I had to go outside the template files and add some code to the actual script files. However, they are pretty well organized and easy to understand as well. Here is a simple example to illustrate how it's done.

At the top left of our site is the current date, generated on the fly by a PHP script. I wanted to add this feature to our template, but unlike the other templates I couldn't simply add PHP code and have it work. Instead, I had to insert code into the processing page, which in the case of CubeCart I went with the "index.php" file. This file handles a lot of global variables, which are available to all the pages of the store (There are also script files for the content pages, in the even that you need specific access to a particular page's source code). So, in order to get the date in the template, I added the following code near the end of the "index.php" file:

$body->assign("HEADER_DATE",date('l, F j, Y'));

What we are doing here is assigning a new global variable (using the same system the developers used for other variables) called HEADER_DATE, and to the value of that variable we adding the current date, displayed as "Monday, January 1, 2007". This assigns the variable that we need so that it will be available for use in our template. I also included a script that creates the code for the navigation, and assigns that code to another variable which will be used in our template.

We then leave that file (don't forget to upload it) and open up the template file that we want to add our date functionality to. Look in your templates files for the "styleTemplates > Global > index.tpl" file to change most of the site. This file is like a combine header/footer file, and in this file I locate where I would like my date to show up. Once found, you simply add:

{HEADER_DATE}

where you would like it, and the value of that variable will appear there. Of course, I also do the same thing to output the nagivation menu code as well. While this method works great, and is relatively easy, it is not ideal because I had to go outside of the template files to make the changes. This means that I need to be careful when doing updates on the shopping cart software. Typically template files are separated for exactly that reason, and I would prefer to make all the needed changes within the confines of my template.

Smarty Templates

Smarty Templates are popular with internet application developers because they make it relatively easy to separate the look of the site from the functionality of the application. They work in a system similar to the CubeCart templates, in that variables are assigned in a script, and then applied to a template and appear something like:

{variable}

However, unlike CubeCart, the smarty templates include a special tag that allows you to insert PHP code and have it be processed. I like that since I can insert raw PHP code into the templates and I don't have to mess with the script files. You get the functionality you want and don't have to worry about updates. So as it turns out, in a Smarty Template you can use the {php} tags instead of the typical <?php> tags, so putting the date into that template looks like:

{php} echo date('l, F j, Y'); {/php}

And that is it. I have to say that once I found out how to do all of these, I felt almost stupid for not being able to just figure it out. Aside from CubeCart, which was a little more involved, all the templates were very easy to add PHP functionality to. Looking back, it took me more time to find the information about each template that I needed than it did to actually add the PHP code in.

Well, hopefully this helps someone out. I think that knowing how to add PHP code to templates opens up a lot of opportunities and possibilities for web designers.

Category: Developers' Corner | Tags: wordpress, php, Shopping Carts and Platforms, Development

2 Comments

Rss-sm

Sign-up to receive EcommerceNotes, our acclaimed email newsletter.

View A Sample | Privacy

Connect with us

Bloggers Wanted

We’re looking for merchants and other ecommerce professionals to share their experiences with our readers. If this interests you, we invite you to contact us.

Help

Featured Tags | All A-Z

 

Inside Practical eCommerce