CARVIEW |
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
- XML-based templating with custom tags for dynamic content
- Database integration with MySQL for data-driven templates
-
Variable replacement system using
%variable%
syntax - Template inclusion for modular template design
- DOCTYPE management for different HTML standards
- Conditional content with ignore blocks
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:
-
type
(required): The DOCTYPE variant to use
Supported Types:
-
html4strict
- HTML 4.01 Strict -
html4trans
- HTML 4.01 Transitional -
xhtml1strict
- XHTML 1.0 Strict
Example:
<doctype type="html4strict" />
Output:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "https://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) - Any additional attributes become available as replacement variables in the included template
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 query
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:
-
sql
(required): The ID of the SQL query to execute
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:
-
%#%
- Provides a counter starting from 1 for each row - Column names from the SQL query become available as
%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>
Variable Replacement System
The template parser supports variable replacement using the %variable%
syntax.
Variable Sources
Variables can come from several sources:
- Constructor replacements - Passed when creating the TemplateParser instance
-
Include attributes - Attributes on
<include>
tags -
Database columns - Column values from SQL queries in
<output>
blocks -
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>© %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):
<img>
<br>
<link>
<meta>
<hr>
Example:
<img src="logo.png" alt="Logo" />
<br />
<hr />
Error Handling
The parser includes built-in error checking:
- XML parsing errors - Invalid XML structure
-
Missing required attributes - e.g.,
<sql>
withoutid
-
Template file not found - Invalid
<include>
references - SQL execution errors - Invalid queries or database connection issues
- Invalid DOCTYPE types - Unsupported DOCTYPE specifications
Best Practices
-
File Organization: Keep templates in the
templates/
directory with descriptive names -
Modular Design: Use
<include>
tags for headers, footers, and reusable components - SQL Optimization: Write efficient queries and use appropriate LIMIT clauses
- Variable Naming: Use clear, descriptive variable names
- Error Testing: Test templates with various data scenarios
- Security: Sanitize any user input before passing to replacement variables
Limitations
- MySQL Only: Currently only supports MySQL database connections
- No Nested Queries: SQL queries cannot reference other SQL results
- No Conditionals: No built-in if/else logic (use ignore blocks for simple cases)
-
No Loops: Only database-driven iteration via
<output>
tags - Case Sensitive: XML tag names are case-sensitive
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.