Template System

Making your templates completely customizable.

View Showcase

Download 1.34 BETA
Includes template system and showcase

MyCustomBB’s template system is a very flexible template system with a variety of uses. This system plays a very large role in making MyCustomBB the adaptive and customizable bulletin board system it claims to be.

What is a Template System?

Many web designers, when creating a web page, simply type out the HTML and call it that. When using a dynamic script, such as PHP, areas of the page may be generated. However, this generally leads to a single page design that can not be altered without either altering the script or the page’s HTML.

Template systems, though a little more complex, give you an incredible amount of flexibility when designing your web page. The most important aspect of the template system is that is separates software developers from theme designers, while still giving the designers enough control to create complex templates without the complex coding the software developer may encounter. Without a template system, they would have to sift though the code and figure out what areas needed to be edited. For example, when you generate a web page with PHP, you need to understand how to read and write PHP code. If the web designer does not understand PHP, they will not be able to make modifications to the web page design effectively or efficiently.

Fields & API

It is considered good practice with the template system to create your fields with the field delimiters (”[” and “]” by default - see showcase for more information). The fields will be generated based off of a template. Field templating allows the fields to match with the current theme (generally represented by a set of templates). This allows allows editing of the fields by use of the template system’s API. Any field generated using the delimiters may be modified, removed, or used as a reference position for the creation of new fields dynamically during page generation (within the PHP code itself). This is an essential feature giving the “Custom” in MyCustomBB a big boost.

Themes

Template systems’ most common use is theming. Developers need only create a single web page that incorporates the template system, and that page will have the ability to support an infinite number of themes, without the need of modifying the code that page is made up with. In the case of MyCustomBB, this allows users to create and modify their own themes to further personalize their bulletin board.

Themes are created by the use of a series of templates. Each page can have its own template, or load a template from another page. Generally, this process consists of loading the header template, the body, and then the footer. The header and footer are generally loaded automatically by the script the developer creates, making each page consistent. The template itself consists of HTML, as well as any programming language the template system incorporates. In the case of MyCustomBB’s template system, the language is very similar to PHP (more information below), but contains a large number of intuitive functions that makes life for template designers a lot easier. These functions drastically reduce template creation time, and make each page consistent. However, learning this language is not required, and templates need only consist of HTML.

Template Language

The template system contains a programming language that allows you to create complex templates without the need of modifying the PHP code for the actual page. This language is very similar to PHP, with very minor differences. In fact, much of the code is only checked to ensure only allowed functions are being used, to convert variables, etc, while the rest is ignored and parsed directly by PHP.

The language is far to extensive to explain in this section, so this article will only provide a brief summary. Code is placed between two delimiters, which are “{” and “}” by default. Any text within those non-escaped delimiters will be parsed. Syntax for this code is generally the same as PHP, with the exception of inline functions. For example, if you would like to bold text, you would type “{ ‘text’ | bold }”. This is exactly the same as typing “bold( ‘text’ )”.

The template system allows certain PHP functions such as string manipulation functions and regular expressions, as well as some of its own functions. The PHP function list can be modified via your script. All other functions not defined in the template system are disallowed (considered not defined) for security reasons. The template functions contain text manipulation functions (such as bolding text), as well as HTML generation functions.

Parsing of the delimiters can be ignored either by using the <noscript> tag, or escaping it with a backslash (”\”).

Template Positions

Positions are what makes the MyCustomBB templates so flexible. Positions are defined by simply typing “{POS_NAME}” - the name of the position in caps between the two delimiters. Once defined, you may use the template system to assign child templates (any other template) to that position. The template will then be included in the page and may contain positions as well. In the case of MyCustomBB, major objects, such as forum listings, user information, menus, etc, are created as child templates and assigned to the position specified via the administration panel, or even per the member’s settings.

Compiling

When template caching is enabled, templates are compiled and cached as PHP code. This drastically decreases parse time, and allows for complex functions that greatly increase the functionality of the template system, such as template modifiers (see below). If caching is not enabled, templates will be compiled every time a page loads before it is displayed, greatly decreasing performance. The template system is designed to be used with caching; if left disabled, it is not recommended that you use any advanced template features.

After a template is compiled and cached, the template system will simply load and immediately parse the template instead of first compiling it. This is also a great aid in debugging template code. If you can’t understand what you’re doing wrong with the template code, you may look at the compiled code (PHP code) that was generated and locate the problem. You can then modify the template code accordingly.

For small templates, the performance increase will be around 50%. However, for larger templates that require longer compilation times, performance increase could be anywhere from 100% to 1000%. Exact figures for the different MyCustomBB templates will be provided upon the first BETA release of the MyCustomBB software.

Modifiers

MyCustomBB’s template system supports a special system that allows for loading of small files called modifiers. Essentially, they are regular expressions executed when a template is compiled. The theme configuration file controls what modifiers should be used for that theme. For example, in the MyCustomBB software there is a modifier that looks for the word “highlight” within a “tr” tag. When found, it replaces it with the necessary code to highlight the entire row on mouse over.

Modifiers can also be used to add tags to the templates. For example, MyCustomBB uses a modifier to create a <javascript> tag. When compiled, the tag is converted to “<script type=”text/javascript”>”. This increases code readability, and decreases file size.

Smilies

It may seem like an odd addition, but why not? MyCustomBB’s template system has the ability to parse smilies within a string via the “smilies” function. Smiley codes and images can be defined in the template system.

These are only a few features of the template system. As it comes closer to release, I’ll be adding more information, and new features will be added as MyCustomBB is developed. This is going to be a very powerful template system that can be used in any project to create truly dynamic web pages.