Compdigitec Labs

« | Home | »

Simple Template Engine – your lightweight templating engine in PHP

By admin | February 2, 2009

The Simple Template Engine is a very lightweight template engine to use in PHP if your webhost does not allow you to run something like Smarty. It is simple, lightweight and fast.

To parse a file:

<?php

// include simple template engine
include("tpl.class.php");

$tpl = new template();

$tpl->assign(Array('content'=>"Hello World from Simple Template Engine!"));

// If you want to display it directly
$tpl->display('example.tpl');

// If you want to send it to another function/class for more processing
some_function($tpl->parse('example.tpl'));

?>

example.tpl is:

<html><head><title>{content}</title></head><body><h1>{content}</h1></body></html>

If you found this article useful or helpful, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more useful and interesting articles!

Topics: (X)HTML, PHP | 2 Comments »

2 Responses to “Simple Template Engine – your lightweight templating engine in PHP”

  1. andrew Says:
    March 4th, 2009 at 8:51 am

    Hi!
    Very nice template system ;)

    I wonder how can I use php code in the template file? Is it possible?

    Thanks

  2. admin Says:
    March 7th, 2009 at 5:38 am

    @andrew:

    Where you would use:

    < ?php
    $s = 5 + 46;
    echo "Test is " . strval($s);
    ?>

    You would use:

    (template code)
    Test is %number%

    (php code)
    < ?php
    include("tpl.class.php");

    $s = 5 + 46;

    $tpl = new template();
    $tpl->assign(Array(‘number’=>strval($s)));
    $tpl->display(‘template.tpl’);

    ?>

Comments