This directory contains examples demonstrating how to use the Markdown Escape PHP library's templating system.
Demonstrates basic template usage including:
- Simple string templates with PHP short tags
- Built-in templates (document, table, list, code-example)
- Basic variable escaping
Shows how to create and use custom templates:
- File-based templates
- User profile template example
- Release notes template example
- Template organization
Covers advanced templating features:
- Custom escapers for special formatting
- Template inheritance with chain loaders
- Dynamic template selection
- Error handling
- Complex data structure rendering
Demonstrates generating API documentation:
- Class documentation templates
- Full API reference generation
- Quick reference cards
- Nested template rendering
Each example is self-contained and can be run from the command line:
# Make sure dependencies are installed
composer install
# Run an example
php examples/01-basic-usage.phpSome examples create template files in the examples/templates/ directory. These are created automatically when you run the examples.
Templates have access to:
$mdor$markdown- The MarkdownEscape instance for escaping$escapers- Object providing access to all registered escapers$context- The template context with all variables- Any variables passed to the
render()method
Always escape dynamic content:
$md->escapeContent()- For general text content$md->escapeUrl()- For URLs in links$md->escapeInlineCode()- For inline code$md->escapeCodeBlock()- For code blocks
Templates can be loaded from:
- Strings (using
renderString()) - Files (using file loader)
- Arrays (using array loader)
- Multiple sources (using chain loader)
The library includes templates for:
document- Structured documents with sectionstable- Markdown tableslist- Bulleted lists with sub-itemscode-example- Code examples with outputlink-list- Lists of linksapi-method- API endpoint documentation
- Create a
.phpfile with PHP short tags - Use
<?= $md->escapeContent($variable) ?>to output escaped content - Use standard PHP control structures for logic
- Save the file in a directory and add it to the template loader
Example template:
# <?= $md->escapeContent($title) ?>
Published: <?= date('Y-m-d') ?>
<?php if (!empty($tags)): ?>
Tags: <?= implode(', ', array_map(function($tag) use ($md) {
return '`' . $md->escapeInlineCode($tag) . '`';
}, $tags)) ?>
<?php endif; ?>
<?= $md->escapeContent($content) ?>- Always escape user content - Use the appropriate escape method for the context
- Use built-in templates - They handle escaping correctly
- Organize templates - Use namespaces for different template categories
- Handle errors - Catch
TemplateNotFoundExceptionandTemplateRenderException - Cache templates - The file loader caches templates automatically
- Test templates - Ensure they handle edge cases and special characters
Check the main documentation for more details on: