View
Templates for CodeScape Framework
About
Why not use X?
(Where "X" is some other template system.)
The main reason not to use other PHP template systems is I feel they generally miss the point - PHP already is a template system! Many template systems "invent" a specific template language. This means having to learn a whole new syntax and library for doing something that PHP can already do.
Instead, I've decided to implement the template features I wanted inside PHP, and have the templates written in HTML with inline PHP.
- Faster: The PHP interpreter is good at running PHP! Templates written in PHP should run very quickly compared to some template-specific language.
- Flexible: PHP already has a wealth of useful libraries at it's disposal, which leaves template writers unencumbered when it comes to getting things just right.
- Easy: If somebody already knows PHP, they already know the template language - no learning curve.
- Simple: No need to implement a complex parser and a large amount of functionality just to end up with a subset of PHP.
As I've seen said in a few discussions on the topic,
"The goal of a template system is to separate your application logic from your presentation logic, not your HTML from your PHP."
Goal
My primary goal was the ability to use hierarchical templates. This idea, for me, came from Django, where it is implemented extremely well in my opinion. In such a system, a template can have "blocks" defined within it, which can be overridden in templates which inherit from it. Blocks can even include the previously-defined value for themselves.
The end result is a very flexible way to template a website, for example having a base template that actually controls the layout of the site, then for a particular area using the content areas of that layout.
Configuration
- csf.view.template_dir
- Template directory - all template paths are relative to this
Usage
Initialising the module
$csf->load_module('view', array('/path/to/templates'));
Using context variables
<?php
// The following two methods are equivalent
$csf->view->title = 'Some title';
$csf->view->set('body', 'A lot of body text to make the world happy');
$output = $csf->view->render('page.php');
?>
<!-- page.php -->
<h1><?= $title; ?></h1>
<p>
<?= nl2br(htmlspecialchars($body)); ?>
</p>
Inheritance, block replacement
<!-- base.php - the base template -->
<? Template::block('foo'); ?>
This is foo in the base template
<? Template::endblock(); ?>
<? Template::block('bar'); ?>
This is bar in the base template
<? Template::endblock(); ?>
<!-- mytemplate.php - the inheriting template -->
<? Template::inherit('base.php'); ?>
<? Template::block('foo'); ?>
This is foo in the child template
<? Template::endblock(); ?>
The output from parsing mytemplate.php is:
This is foo in the child template This is bar in the base template
Notice how the block "foo" in the child template replaced the content of the same block in the base template.
Extending blocks
<!-- base.php -->
<? Template::block('foo'); ?>
This is foo in the base template
<? Template::endblock(); ?>
<!-- mytemplate.php -->
<? Template::inherit('base.php'); ?>
<? Template::block('foo'); ?>
This is foo in the child template
<? Template::blocksuper(); ?>
<? Template::endblock(); ?>
The output from parsing mytemplate.php is:
This is foo in the child template This is foo in the base template