Streamlining Your Templates (A Module Developer's Approach To Theming)

As a module developer, I spend most of my time developing and maintaining well, modules. My background mainly consist of working with languages such as, C/C++, Perl, etc., and building modules with PHP is right up my alley. The main difference between module development and theming is the type of code that is used on each layer. I'm not as used to building html templates with interspersed PHP as I am going full PHP and even then, using that to generate HTML as opposed to writing the HTML, myself.

Well, with our current project, I was given the task of creating a template for one of the main content types of the site. Not a problem.

I took a copy of a similar template that we are using on a different site as reference and began to convert it to suit the requirements of the new theme. As I made my way through the process I realized that there was just too much code in this template to be processed on the theme layer! So, I needed to clean up and streamline this badboy to help it be more concise and organized (not to mention easy on the eyes).

When breaking down a template you want to look for these few things:

  • Is there a mass of php or javascript being used to generate media?
  • Are there utility components like toolbars or story commands, etc. that are hardcoded but, other content types might use?
  • any deprecated code/tags?
  • Are there more div containers than really necessary?

There may be more things that can come up but, the list above are things that many people don't think about when building template, etc., and taking those things and cleaning them up a bit can really improve your code and that's a good thing especially when others may have to come behind you.

Taking that list into consideration, let's see how we can clean that stuff up.

If you have a lot of script in your template to generate media slideshows or the like, see if you can take that out and consolidate it into a separate function(s). In the template that I was working on, I found that we were generating a photo slideshow and embedding video and on top of that we were using jQuery to add that content to a tabbed block... all in the template.

To clean this up I created a module to house my special theme functions and then i created a theme function to handle all of that code on the module level and in the template all I had to do was just print that function. To tell the truth, doing that in itself, took about 2kb off of the template file.

While we're on the subject of consolidating code in functions, let's think about those things such as, utility bars and story commands, etc.. What you want to think about with these types of template features is re-usability. Are there other content types that may use the same thing? Think of it this way. If it's something that you're going to use in many different places, why write it more than once? Reinventing the wheel can really bog you down when get caught up in deadlines and such.

Take your utility items and put them in one theme function; that way it's available to all of your templates and you only have to write it once.

Building new templates or even rebuilding old templates is a great chance to clean out deprecated code and the "ol' trusty" code that just seems to stick around. It's always a good idea to take the time during development to make sure that your code is compliant with current standards. This helps give your code longer life and also, helps minimize the cross browser bugs that you will have to chase when they pop up down the road.

And finally, at least in terms of this article, avoid using too many div containers. There's really no reason for it. I've seen markup code that was just infested with div's and it even got to the point where just one single closing div tag caused a nightmare and a few late nights at the office. When it comes to div's use as necessary. This helps not only with helping your template markup be cleaner and easier to read. It helps keeps your css more streamlined as well; a great thing when it comes time for styling changes and maintenance.

There are many things that you can do to clean up your code. The things above that I mentioned are some of the main things that I have found over time that really help especially, the consolidation of code. Building your own theme functions is even a great way for module novices to learn a little about the module data layer as well.

Give your templates a once-over with these things in mind and see if you can things in your code that could be tidied up a tad and of course if you find some things that I did not mention feel free to contribute your ideas to the site.

1 comments on Streamlining Your Templates (A Module Developer's Approach To Theming)

  1. Anonymous
    Wed, 04/29/2009 - 10:41
  2. Post new comment

    The content of this field is kept private and will not be shown publicly.
    • Web page addresses and e-mail addresses turn into links automatically.
    • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
    • Lines and paragraphs break automatically.
    • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

    More information about formatting options

    CAPTCHA
    If you are if fact human, you may post comments.
    Image CAPTCHA
    Enter the characters shown in the image.