PHP XTemplate: Import Files

In the previous two tutorials on XTemplate, I discussed the basics of tag assignment and dealing with dynamic blocks, but the real power of using a templating system comes when you are able to use the same basic layout template on more than one page while displaying different content. XTemplate allows us to import template files into an existing template, allowing us to maintain a site structure on each page, but the true power comes when we need to make changes to the layout. Edit one file and the changes go into effect for all the pages using that template.

How It Works

Let’s start with a simple layout template:

<!-- BEGIN: main -->
<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>{TITLE}</title>
</head>

<body>
    <h1>{TITLE}</h1>
    <section id="main">
        {FILE {FILENAME}}
    </section>
</body>
</html>
<!-- END: main -->

Our template is a very basic HTML5 layout. we have two tags in the template:

  1. {TITLE} – this will add a title to our page
  2. {FILE {FILENAME}} – this is where we will insert a template snippet contained in another file

Save the file as main.tpl.

The Snippet Templates

The first snippet is for our index page:

<p>This is the {TEMPLATE} template...</p>
<p><a href="second.php">Link to second...</a></p>

Notice that it has one tag, {TEMPLATE}. When we import this snippet into out main template, the snippet becomes part of the main block. We can assign values to the tags as though the had been a part of the main template file.Save the first snippet as first.tpl.

Now, our second snippet:

<p>This is the {TEMPLATE} template...</p>
<p><a href="index.php">Link to first...</a></p>

The second snippet is much like the first, except for the link is different. Save it as second.tpl.

The PHP Code

We will create two PHP files: index.php and second.php.

index.php

<?php
require_once('xtemplate.class.php');

$xtpl = new XTemplate('main.tpl');

$xtpl->assign('TITLE', 'Importing Templates from File');
$xtpl->assign_file('FILENAME', 'first.tpl');
$xtpl->assign('TEMPLATE', 'first');
$xtpl->parse('main');
$xtpl->out('main');
?>

second.php

<?php
require_once('xtemplate.class.php');

$xtpl = new XTemplate('main.tpl');

$xtpl->assign('TITLE', 'Importing Templates from File');
$xtpl->assign_file('FILENAME', 'second.tpl');
$xtpl->assign('TEMPLATE', 'second');
$xtpl->parse('main');
$xtpl->out('main');
?>

We start off by requiring the XTemplate class file (line 2). I used require_once because if we cannot file the XTemplate class, there not much point in continuing. We create a new XTemplate object using the main.tpl template (line 4). We assign a value to the TITLE tag (line 6).

Line 7 is where we start with something new. We use the XTemplate method assign_file to assign the snippet file to the to FILE tag FILENAME. Once we assign the file, we assign values to its tag(s) (line 8).

Finally, we parse the template and spit it out to the web browser (lines 9-10).

While this was a simple example, your template snippets can be as simple or complicated as your needs. The snippet files can contain other blocks, as well as dynamic blocks, which you can handle just like we discussed previously.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s