Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Fast PHP Template Inheritance

branch: master

Fetching latest commit…


Cannot retrieve the latest commit at this time

Octocat-spinner-32 examples
Octocat-spinner-32 LICENSE
Octocat-spinner-32 stitch.php

Stitch - Fast Template Inheritance

Most websites out there have multiple pages using a single template. Maintaining web pages as raw .html pages are more difficult as number of pages increase, since any change to the template must be manually edited on all pages.

Template inheritance seeks to solve this problem by keeping your template and page contents in separate files. When a user visits your page, a script is called which stitches the page and template togeather, and serves the final page.

Supported PHP versions

Tested on 5.3.10 and 5.2.

Usage with an example

Quick Start


<?php include('stitch.php'); ?>
<!DOCTYPE html>
        <div style="background-color:#f0f0f0;">
            <?php defineblock('main-content'); ?>


<?php include('Template.php'); ?>

<?php startblock('main-content') ?>
    Here is my home page's main body content.
<?php endblock() ?>

Default content

Modify Template.php:

<div style="background-color:#f0f0f0;">
    <?php startblock('main-content'); ?>
        This is the default content. If page chooses not to override this block, then the default content is shown.
    <?php endblock(); ?>

Append/Prepend content

Most pages choose to override content. But on some cases, a page may need to append/prepend content.


    <?php startblock('font'); ?>
        <link type="text/css" rel="stylesheet" href=",700" />
    <?php endblock(); ?>


    <?php startblock('font', TI::APPEND); ?>
        <!-- Appends a font -->
        <link type="text/css" rel="stylesheet" href="" />
    <?php endblock(); ?>


    <?php startblock('font'); ?>
        <!-- Doesn't require the Droid Sans font, hence replace it with a different one -->
        <link type="text/css" rel="stylesheet" href="" />
    <?php endblock(); ?>

Macros and Partial Views

Can easily be achieved with plain vanilla PHP.


<?php include('macros.php') ?>
    //Include a view
    //Code-generated html
    renderMenu(array('current_page' => 'Home')); //Where renderMenu echos/prints the html for the menu


Use only on development:

define('TI_PROFILE', true);

This will give you the total time taken to process the page as an apache header with 'X-ti-' prefix. The time shown includes the time taken for php engine to parse the pages.


Some ways to reduce server load and improve page load are 1. to use cache control headers while serving the pages, 2. speed things up with APC.

If you want the output of Stitch on run-time you could use the following function:

function getHtml($pathToPage) {
    ob_end_flush(); //Ends Stitch
    return ob_get_clean();

With this you can compile the html to a static .html file and then take that further to use cache control. Or with dynamic content, cache content with in-memory storage like memcached and echo them out within the page/template blocks?

There are many ways to do this, so pick what is best for your application.

Philosophy and Features

Q. Why don't you support nested blocks? Why can't page inherit pages or multiple templates? Why can't a template inherit another template?

A: Philosophical reasons:

  1. To keep the code simple and to keep template processing overhead to the minimum.

  2. I don't see why would you need them. In most cases, you can organizing your code in a way that you don't need them.

On nesting: If you really need it, you can look into PHPTI.

Something went wrong with that request. Please try again.