My favorites | Sign in
Project Home Downloads Wiki Source
Search
for
Overview  
Understanding the basic concepts of TemplarPHP.
Featured
Updated Oct 23, 2009 by 03sjbr...@gmail.com

Introduction

On any given website, pages will share common presentational elements--navigation menus, style sheets, copyright notices, etc. TemplarPHP provides a way to move these elements into a separate file called a template. Separating presentation from content makes a website easier to build, manage and update over time. When a user requests a webpage, TemplarPHP merges the content page together with the template and the result is sent to the user's web browser.

In fact, TemplarPHP can stack multiple templates on top of each other to create sophisticated presentation schemes for different sections of a website. Template stacks are determined automatically by a website's folder structure. This stacking behavior is what makes TemplarPHP a cascading template framework. A template in the root folder of a website applies to all of a site's web pages. A template in the artwork/ folder applies to web pages in that folder or any of its subfolders. Exceptions to this cascade behavior can be defined on a page-by-page basis as needed.

Template Pages

Template pages use special tags containing variable names. These variables reference specific parts of the content file's HTML code. In the example below, the variables title and body are used. It's possible to define more variables, but these two are built-in and always available.

When TemplarPHP merges the content and template files together, the special tags are replaced with markup from the content file. The title variable gets the markup inside content's title element. The body variable gets the markup inside the content's body element. The combined result is sent to the user's web browser.

Also, notice that the content file's meta-tag (in green) was merged into the result. All of the elements inside the content's <head>...</head> tags (except title) are automatically merged together with those of the template. In the case of conflicting tags, the template's tag takes precedence by default.

Controller Script

A short script is required to load TemplarPHP and control the cascade/merge behavior in response to web page requests. This controller script should be in the root of a site's public web folder. It needs to contain the following lines:

include('templar.php');               // 1
$templar_main = new TemplarPHP();     // 2
$request = $_SERVER['REQUEST_URI'];   // 3
$templar_main->loadRequest($request); // 4
$templar_main->buildResponse();       // 5
$templar_main->sendResponse();        // 6

Assume this is the controller script for a website of famous poems at http://OurPoetrySite.com/. To start, it includes the templar.php script and instantiates a new TemplarPHP object. (lines 1 and 2).

Next, the script gets the REQUEST_URI value (line 3). REQUEST_URI is an "environment variable" provided by the webserver. When setting-up TemplarPHP, the server was configured to load the controller script for certain requests. This way, the script is called with each request but REQUEST_URI changes to match the web page address.

For the address http://OurPoetrySite.com/poe/dreams, the REQUEST_URI value is /poe/dreams. This value is passed to the loadRequest() method (line 4). At this point, TemplarPHP automatically builds a resource stack and finds the following files:

  1. /poe/dreams.html (the content file)
  2. /poe/_template.html
  3. /_template.html

These resource files are merged together and the response headers are prepared (line 5).

Finally, the script sends the output to the user (line 6).

Website Design

This cascade-and-merge behavior is used to develop websites that are easy to maintain, update and migrate to different hosting services (at need). The following illustration shows a set of templates and content files (in the yellow box). Arrows point to the merged results at the bottom.

Comment by st.Ell...@gmail.com, Nov 18, 2009

hallo do you have a emxaple with one working site with it, that gives a better idea greetings Pieter

Comment by st.Ell...@gmail.com, Nov 18, 2009

I forget to mention it looks good yustme again Pieter.

Comment by project member 03sjbr...@gmail.com, Nov 18, 2009

This is a good point -- I'll add some online demos when the stand-alone website is ready. For now, you can take a look at the following temporary site...

On the front of the site - http://sparqlize.org/index - you see the output created using the following resource stack:

  1. http://sparqlize.org/index.html
  2. http://sparqlize.org/_common.php
  3. http://sparqlize.org/_full.html

Using the default template (named 'template') the resource stack would look like the following:

  1. http://sparqlize.org/index.html
  2. http://sparqlize.org/_template.html

But this temporary site is configured to serve content for mobile devices. In this case, the controller script uses the following if/else to set the cascade resources before loading the request:

if ($templar_main->isMobileAgent()) {
    $templar_main->cascadingTemplates = array('common', 'mobile');
} else {
    $templar_main->cascadingTemplates = array('common', 'full');
}

I've left the logging turned on. So when loading the compiled version (http://sparqlize.org/index), you can view-source and see the TemplarPHP log in HTML comments near the bottom of the <head> tag.


Sign in to add a comment
Powered by Google Project Hosting