Main Menu

· Home
· Benchmarks
· Documentation
· Download
· License
· Thanks

Advanced Topics

This section of the documentation simply discusses some more advanced usages. Please read the other sections of the documentation first, as you need to be familiar with bTemplate before moving into these topics.

Master Templates

The idea of a "master template" is that you have the entire design of the site (minus the content) in one template file. This is different than most template systems in which you split the main design into a top and bottom, and then include both of those. This results in fragmented templates that can't be edited in most WYSIWYG editors (unless, of course, you feel like joining and splitting the two files each time you need to modify the design).

bTemplate doesn't specifically deal with this problem, but with a little bit of creativity, it's easy to overcome. First take a look at the master template.

master.tpl:

<html>
  <head>
    <title><tag:title /></title>
  </head>

  <body>
    <tag:content />
  </body>
</html>

This is a very simple example of a master template, but remember, the template should contain every bit of your layout except dynamic features (logged in messages, status notification, form results, etc.) and the main content of the page. What we're doing is defining a "content area" that will be used to set the content through the PHP script.

This is handy if you get your data from a dynamic source such as a database. However, if you simply have a bunch of static pages and you're looking for an easier way to update the layout, you can also pull the data in from separate files with minimal HTML tags. Let's look at a couple of examples.

Populating From a Static File

While I don't recommend doing this (as it's obvious there is data mixed with HTML in this example), it's important to understand that you can parse the results of an external file (or even another template), and set it to a variable in another template (like the content variable we defined above).

article.html:

<h2>PHP Programming</h2>
<p>PHP is a recursive acronym for PHP: Hypertext Preprocessor. PHP is an interpreted language that is mostly used for developing web applications. Web applications are simply programs that run on a web server and are interfaced with by web clients (web browsers, in most cases).</p>

If we want to take that article and insert it into our design, we simply use the fetch() method of bTemplate.

<?php
include_once('bTemplate.php');
$tpl = new bTemplate();

$title = 'My Article';
$tpl->set('title', $title);

$tpl->set('content', $tpl->fetch('article.html'));

echo $tpl->fetch('master.tpl');
?>

The resulting output would look like this.

<html>
  <head>
    <title>My Article</title>
  </head>

  <body>
<h2>PHP Programming</h2>
<p>PHP is a recursive acronym for PHP: Hypertext Preprocessor. PHP is an interpreted language that is mostly used for developing web applications. Web applications are simply programs that run on a web server and are interfaced with by web clients (web browsers, in most cases).</p>
  </body>
</html>

Populating From a Database

Let's say, for example, that the database table has a structure and data like this.

Table users:

id last_name first_name phone_number
1 Ness Mike (123) 555-1212
2 Hetfield James (456) 555-1212
3 Jovavich Milla (789) 555-1212
4 Portman Natalie (012) 555-1212

Our template for displaying such data would look like this. Please note that I'm intentionally leaving out the opening and closing HTML, HEAD, and BODY tags, as the idea behind this document is to show the use of master templates.

users.tpl:

<table border="1">
  <tr>
    <td><b>id</b></td>
    <td><b>last_name</b></td>
    <td><b>first_name</b></td>
    <td><b>phone_number</b></td>
  </tr>
  <loop:users>
    <tr>
      <td><tag:users[].id /></td>
      <td><tag:users[].last_name /></td>
      <td><tag:users[].first_name /></td>
      <td><tag:users[].phone_number /></td>
    </tr>
  </loop:users>
</table>

The PHP script to populate the array and display the template should look something like this. Obviously you need to connect to the database and do all your normal error-checking routines.

<?php
// Assuming you've already connected to the database
$result = mysql_query('SELECT * FROM users');

// This loop populates an array with the data from the query
while($row = mysql_fetch_assoc($result)) {
  $users[] = $row;
}

include_once('bTemplate.php');
$tpl = new bTemplate();

$title = 'Database Results';
$tpl->set('title', $title);

// Now we set the array we populated
$tpl->set('users', $users);

// And here we fetch the users.tpl file and set it to content
$tpl->set('content', $tpl->fetch('users.tpl'));

echo $tpl->fetch('master.tpl');
?>

The final output from this script should look like this.

<html>
  <head>
    <title>My Article</title>
  </head>

  <body>
<table border="1">
  <tr>
    <td><b>id</b></td>
    <td><b>last_name</b></td>
    <td><b>first_name</b></td>
    <td><b>phone_number</b></td>
  </tr>
    <tr>
      <td>0</td>
      <td>Ness</td>
      <td>Mike</td>
      <td>(123) 555-1212</td>
    </tr>
    <tr>
      <td>1</td>
      <td>Hetfield</td>
      <td>James</td>
      <td>(456) 555-1212</td>
    </tr>
    <tr>
      <td>2</td>
      <td>Jovavich</td>
      <td>Milla</td>
      <td>(789) 555-1212</td>
    </tr>
    <tr>
      <td>3</td>
      <td>Portman</td>
      <td>Natalie</td>
      <td>(012) 555-1212</td>
    </tr>
</table>
  </body>
</html>