Documentation for my 2003 experimental PHP template engine, generated by Claude in July 2025.
The Template Parser is a PHP-based XML template processing system that combines HTML generation with database integration. It allows you to create dynamic web pages by embedding SQL queries and variable replacements directly into XML template files.
%variable%
syntax<?php
// Initialize the template parser
$db = mysql_connect('localhost', 'username', 'password');
mysql_select_db('database_name', $db);
$replacements = array(
'site_name' => 'My Website',
'year' => date('Y')
);
$parser = new TemplateParser($db, $replacements);
// Parse a template file
$template_content = file_get_contents('templates/page.xml');
$html_output = $parser->parse($template_content);
echo $html_output;
?>
Template files are XML documents with a .xml
extension stored in the templates/
directory. They must be well-formed XML with a root <template>
element.
<template>
<doctype type="html4strict" />
<html>
<head>
<title>%site_name%</title>
</head>
<body>
<!-- Your content here -->
</body>
</html>
</template>
<doctype>
Outputs standard HTML DOCTYPE declarations.
Attributes:
type
(required): The DOCTYPE variant to useSupported Types:
html4strict
- HTML 4.01 Stricthtml4trans
- HTML 4.01 Transitionalxhtml1strict
- XHTML 1.0 StrictExample:
<doctype type="html4strict" />
Output:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<include>
Includes another template file, enabling modular template design.
Attributes:
template
(required): Name of the template file (without .xml extension)Example:
<include template="header" page_title="Welcome" />
This includes templates/header.xml
and makes %page_title%
available within that template.
<sql>
Defines a SQL query that can be referenced later by <output>
tags.
Attributes:
id
(required): Unique identifier for this SQL queryExample:
<sql id="users">
SELECT id, name, email FROM users WHERE active = 1 ORDER BY name
</sql>
<output>
Executes a previously defined SQL query and outputs the results by repeating a block of content for each database row.
Attributes:
sql
(required): The ID of the SQL query to executeExample:
<sql id="articles">
SELECT title, content, author, DATE_FORMAT(created, '%M %d, %Y') as date
FROM articles WHERE published = 1 ORDER BY created DESC LIMIT 10
</sql>
<output sql="articles">
<div class="article">
<h2>%title%</h2>
<p class="meta">By %author% on %date%</p>
<div class="content">%content%</div>
<p class="counter">Article #%#%</p>
</div>
</output>
Special Variables in Output Blocks:
%#%
- Provides a counter starting from 1 for each row%column_name%
variables<ignore-start>
and <ignore-stop>
Creates blocks of content that are completely ignored during processing. Useful for comments or temporarily disabling sections.
Example:
<ignore-start>
This content will not appear in the final output.
<p>Even HTML tags are ignored here.</p>
</ignore-start>
<p>This content will appear normally.</p>
<ignore-stop>
<!-- This is redundant but won't cause errors -->
</ignore-stop>
The template parser supports variable replacement using the %variable%
syntax.
Variables can come from several sources:
<include>
tags<output>
blocks%#%
for row counters$replacements = array(
'site_name' => 'My Blog',
'copyright_year' => '2024',
'admin_email' => 'admin@example.com'
);
$parser = new TemplateParser($db, $replacements);
<template>
<h1>Welcome to %site_name%</h1>
<footer>
<p>© %copyright_year% - Contact: %admin_email%</p>
</footer>
</template>
CREATE TABLE news (
id INT PRIMARY KEY,
headline VARCHAR(255),
summary TEXT,
author VARCHAR(100),
published_date DATE
);
templates/news.xml
)<template>
<doctype type="html4strict" />
<html>
<head>
<title>%site_name% - Latest News</title>
<meta name="description" content="Latest news from %site_name%" />
</head>
<body>
<include template="header" current_page="news" />
<div id="content">
<h1>Latest News</h1>
<sql id="recent_news">
SELECT headline, summary, author,
DATE_FORMAT(published_date, '%M %d, %Y') as formatted_date
FROM news
WHERE published_date <= CURDATE()
ORDER BY published_date DESC
LIMIT 5
</sql>
<output sql="recent_news">
<article class="news-item">
<h2>%headline%</h2>
<p class="meta">By %author% on %formatted_date%</p>
<p class="summary">%summary%</p>
<a href="news.php?id=%id%">Read more...</a>
</article>
<ignore-start>
<!-- Article #%#% - useful for debugging -->
</ignore-start>
</output>
</div>
<include template="footer" />
</body>
</html>
</template>
<?php
$db = mysql_connect('localhost', 'user', 'pass');
mysql_select_db('mysite', $db);
$globals = array(
'site_name' => 'Tech News Daily',
'current_year' => date('Y')
);
$parser = new TemplateParser($db, $globals);
$output = $parser->parse(file_get_contents('templates/news.xml'));
echo $output;
?>
The parser processes standard HTML tags normally, with special handling for self-closing tags:
Self-closing tags (automatically closed):
<img>
<br>
<link>
<meta>
<hr>
Example:
<img src="logo.png" alt="Logo" />
<br />
<hr />
The parser includes built-in error checking:
<sql>
without id
<include>
referencestemplates/
directory with descriptive names<include>
tags for headers, footers, and reusable components<output>
tagsThis template system uses deprecated mysql_*
functions. For modern PHP applications, consider updating to use PDO or MySQLi with prepared statements for better security and compatibility.