Clarity Template Examples
This directory contains runnable example templates demonstrating Clarity features from basic to advanced usage.
Running the Examples
Quick Start
- Set up Clarity:
composer install
- Run the example renderer:
php render.php 01-hello
Or render all examples:
php render.php --all
Manual Rendering
<?php
require __DIR__ . '/../../vendor/autoload.php';
use Clarity\ClarityEngine;
$engine = new ClarityEngine();
$engine->setViewPath(__DIR__);
$engine->setCachePath(__DIR__ . '/../../cache/examples');
// Render an example
echo $engine->render('01-hello', [
'message' => 'Hello, Clarity!',
'userName' => 'Developer'
]);
Examples Overview
01-hello.clarity.html
Basics: Variables and Output
Demonstrates:
- Simple variable output
- Auto-escaping
- Dot notation for nested data
02-filters.clarity.html
Filters: Transform Data
Demonstrates:
- Text filters (upper, lower, capitalize, trim)
- Number formatting
- Date formatting
- Filter chaining
03-conditionals.clarity.html
Control Flow: Conditionals
Demonstrates:
- if/else/elseif statements
- Comparison operators
- Logical operators (and, or, not)
- Ternary operator
04-loops.clarity.html
Control Flow: Loops
Demonstrates:
- for loops over arrays
- Loop variables (index, first, last)
- Range loops (exclusive and inclusive)
- Loops with step
- Nested loops
05-layout.clarity.html & 05-page.clarity.html
Template Inheritance
Demonstrates:
- Defining layouts with blocks
- Extending layouts
- Overriding blocks
- Multiple blocks
06-complex.clarity.html
Real-World: Blog Post Listing
Demonstrates:
- Combining multiple features
- Realistic data structure
- Filters with arrays (map, filter)
- Includes and layouts together
- Conditional rendering
_components/
Reusable Components
Small reusable template fragments:
header.clarity.html— Site headerfooter.clarity.html— Site footernav.clarity.html— Navigation menuuser-card.clarity.html— User profile card
Example Data
Each example uses realistic sample data. See render.php for the data structure passed to each template.
Learning Path
For Beginners:
- Start with
01-hello.clarity.html— understand basic syntax - Move to
02-filters.clarity.html— learn data transformation - Try
03-conditionals.clarity.html— add logic - Practice
04-loops.clarity.html— work with collections
For Intermediate Users: 5. Study 05-layout.clarity.html + 05-page.clarity.html — master inheritance 6. Examine 06-complex.clarity.html — see realistic usage 7. Explore _components/ — learn reusability patterns
Modifying Examples
Feel free to modify these examples to experiment:
# Edit an example
nano 02-filters.clarity.html
# Clear cache to see changes
rm -rf ../../cache/examples/*
# Re-render
php render.php 02-filters
Creating Your Own Examples
Add your own example template:
File: my-example.clarity.html
<!DOCTYPE html>
<html>
<head>
<title>My Example</title>
</head>
<body>
<h1>{{ title }}</h1>
</body>
</html>
Render it:
echo $engine->render('my-example', ['title' => 'Testing']);
Common Issues
Template Not Found
Ensure you're in the docs/examples/ directory when rendering, or set the correct view path.
Cache Permission Errors
Make sure cache/examples/ directory exists and is writable:
mkdir -p ../../cache/examples
chmod 755 ../../cache/examples
Changes Not Appearing
Clear the cache:
rm -rf ../../cache/examples/*
Or programmatically:
$engine->flushCache();
Next Steps
- Getting Started Guide — Complete setup guide
- Template Syntax Reference — All syntax features
- Filters Reference — Complete filter list
- Best Practices — Code organization tips