Faster and Easier Templates with PHP

Web Programming Add comments

SuperEasyPHPTemplate system is a quick way to build powerful template functionality into web projects with a minimal learning curve. And yes, it’s free!

I wrote it when I was in the middle of a large project and I was not satisfied with the footprints, performance and complexity of popular PHP template systems.

SuperEasyPHPTemplate allows you to put placeholders in normal HTML files and then replace those placeholders with static text, output from PHP functions, or other files including external PHP files.

SuperEasyPHPTemplate allows you to quickly and easily separate your presentation from your logic. In terms of MVC programming (model/view/controller), SuperEasyPHPTemplate provides a means of preparing the View.

To use SuperEasyPHPTemplate, first you need to create one or more template files. A template file can be any HTML file. In terms of the file extension (.HTML or .HTM), you can use anything you want and even different extensions for different files. However, I recommend that you pick an extension and stick to it - to maintain consistency and your own sanity!

To embed a placeholder in a template file, simply enclose it within curly brackets. For example, let’s create a template called “basic.html” which includes placeholders called “page-header”, “page-date”, “page-title”, “page-author”, and “page-footer”:

<body>

{page-header}

Today’s Date is: {page-date}

The title of this web page is: {page-title}

The author of this web page is; {page-author}

{page-footer}

</body>

In order to build a page from our template using SuperEasyPHPTemplate, simply add a reference to SuperEasyPHPTemplate at the top of your own PHP script. SuperEasyPHPTemplate will then allow you to build a page as an object. You can specify each placeholder that you want to change using the replace_tags function of your page object. For example, let’s change the “page-date” placeholder to show the current date using a PHP function, the “page-title” placeholder to read “My First SuperEasyPHPTemplate page” and the “page-author” placeholder to read “ My Name goes here.” In addition, we will change the “page-header” placeholder to the contents of another HTML file that we have already written and the “page-footer” placeholder to the output of another PHP file that we have already written:

<?php

require_once(“SuperEasyPHPTemplate.php“);

$myvariable = “My Name goes here”;

// Create a new template page object based on a specified template.

// If you do not specify a template, SuperEasyPHPTemplate will look for

// a file named “template.html” by default

$myownpage = new Page(”basic.html“);

// change the placeholders in the template using the replace_placeholders function

$myownpage->replace_placeholders(array(

// convert a placeholder to another HTML file

“page-header” => “myheader.html”,

// convert a placeholder to the result of a PHP function

“page-date” => date(“F j, Y”),

// convert a placeholder to some static text

“page-title” => “My First SuperEasyPHPTemplate page”,

// convert a placeholder to a variable

“page-author” => $myvariable,

// convert a placeholder to the output of another PHP file

“page-footer” => “myfooter.php” ));

// show the final page!

$myownpage->output();

?>

Finally, simply call your own PHP script and your finished page will magically appear. Now that’s super and easy.Visit the Downloads page to get SuperEasyPHPTemplate.

Bookmark on del.icio.us
Bookmark on del.icio.us

4 Responses to “Faster and Easier Templates with PHP”

  1. Peter W.P. Says:

    This is a great PHP template system with the tiniest amount of code. Smarty is awesome, but this can do a lot and is tiny = fast. I could see caching being added to this easily. Thanks for sharing it!

  2. corpuser Says:

    just wanted to let you know that this template system is the best I have used and so incredibly easy and flexible
    can’t believe the code is so small but its great because it means my sites are not going to be bogged down

  3. 4onthefloor Says:

    Thanks for this: quick, simple, painless. I had it working in under 10 minutes.

  4. greenley Says:

    thanks this is just what I was looking for, fast and lightweight

Leave a Reply

©2007-2010 Derek Underwood. All rights reserved.
Entries RSS Comments RSS Login