With the advertising out of the way, let's take a simple example to see how patTemplate works. Consider the following template: The block above represents a single template, identified by the opening and closing <patTemplate:tmpl> tags, and by a unique name ("message", in this case). Within the opening and closing tags comes the actual template body; to all intents and purposes, this is a regular HTML document, except that the markup also contains some special patTemplate variables, written in uppercase and enclosed within curly braces. These special variables will be replaced with actual values once the template engine gets its mitts on it. Let's look at that next.{mospagebreak title=Anatomy Of A Template Engine} Next, it's time to initialize the template engine and have it populate the template created on the previous page with actual data. Here's how: This might seem a little complicated, so let me dissect it for you: 1. The first step is, obviously, to include all the relevant files for the template engine. Since patTemplate is packaged as a single class, all I need to do is include that class file. Once that's done, I can safely create an object of the patTemplate class. This object instance will serve as the primary control point for the template engine, allowing me to do all kinds of nifty things. 2. Next, the object's setBaseDir() and readTemplatesFromFile() methods are used to point the engine in the direction of the templates to be read. The setBaseDir() method sets the default location for all template files; the readTemplatesFromFile() method specifies which template file to process. patTemplate allows multiple templates to be stored in the same physical file; the engine uses each template's name to uniquely identify it. 3. Once all the templates in the specified file have been read, it's time to do a little variable interpolation. This is accomplished via the addVar() object method, which attaches values to template variables. In English, this translates to "find the template named 'message' and assign the value 'Error' to the placeholder {TITLE} within it ". You can run addVar() as many times as you like to perform variable interpolation. 4. Once you're done with all the variable replacement, all that's left is to display the final product. The DisplayParsedTemplate() object method parses the specified template, replacing all variables within it with their specified values, and outputs it to the output device. In this specific example, the call to DisplayParsedTemplate() could also be broken down into two separate components, which combine to produce an equivalent result. In this version, the call to parseTemplate() merely parses the named template and replaces variables within it with appropriate values, while the call to getParsedTemplate() gets the contents of the parsed template and places it in a string variable. As you will see, this alternative version has its uses, especially when it comes to iteratively building or repeating a Web page. This is discussed in detail a little further along - for the moment, just feast your eyes on the result of all the work above: ![]() The nice thing about this approach? The page interface and page elements are separated from the program code that actually makes the page function - and can therefore be updated independently by Web designers and PHP developers.
blog comments powered by Disqus |