Skip to content
Fast PHP Template Inheritance
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

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, stitch.php will stitch the page and template togeather, and serve the final page.

This is not like ESI includes, because with ESI, a "template" can only include one html file (or a hardcoded few if you use esi:when). With Stitch multiple pages can reuse a template and can also override, prepend or append content to any template "block", which ultimately helps in cleaner code ("seperation of concerns").

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 HTTP header with 'X-ti-' prefix. The time shown includes the time taken for php engine to parse the pages.

Optimization Tips

Stitch takes ~1 ms to render most pages. However you can further reduce render overhead by using cache control HTTP headers while serving the pages.

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 could write the html to a static .html file. Or with dynamic content, you can cache the content with in-memory storage like memcached and echo them out within the page/template blocks? There are many ways to do page optimization, 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.