2003-era PHP template engine documentation

Documentation for my 2003 experimental PHP template engine, generated by Claude in July 2025.

Overview

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.

Key Features

Getting Started

Basic Setup

<?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 File Structure

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>

Template Tags Reference

<doctype>

Outputs standard HTML DOCTYPE declarations.

Attributes:

Supported Types:

Example:

<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:

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:

Example:

<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:

Example:

<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:

<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>

Variable Replacement System

The template parser supports variable replacement using the %variable% syntax.

Variable Sources

Variables can come from several sources:

  1. Constructor replacements - Passed when creating the TemplateParser instance
  2. Include attributes - Attributes on <include> tags
  3. Database columns - Column values from SQL queries in <output> blocks
  4. Special variables - Like %#% for row counters

Example

$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>&copy; %copyright_year% - Contact: %admin_email%</p>
    </footer>
</template>

Complete Example

Database Table

CREATE TABLE news (
    id INT PRIMARY KEY,
    headline VARCHAR(255),
    summary TEXT,
    author VARCHAR(100),
    published_date DATE
);

Template File (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 Usage

<?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;
?>

HTML Tag Handling

The parser processes standard HTML tags normally, with special handling for self-closing tags:

Self-closing tags (automatically closed):

Example:

<img src="logo.png" alt="Logo" />
<br />
<hr />

Error Handling

The parser includes built-in error checking:

Best Practices

  1. File Organization: Keep templates in the templates/ directory with descriptive names
  2. Modular Design: Use <include> tags for headers, footers, and reusable components
  3. SQL Optimization: Write efficient queries and use appropriate LIMIT clauses
  4. Variable Naming: Use clear, descriptive variable names
  5. Error Testing: Test templates with various data scenarios
  6. Security: Sanitize any user input before passing to replacement variables

Limitations

Migration Notes

This 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.