PHP Classes

Simplate: Template engine that replaces arrays of variables

Recommend this page to a friend!
  Info   Example   Screenshots   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 191 All time: 8,583 This week: 41Up
Version License PHP version Categories
simplate 1.0BSD License5.0PHP 5, Templates
Description 

Author

This class implements a template engine that replaces arrays of variables.

It can load and parse a template file from a given directory and traverses an associative array of variables to process the template file replacing the template marks by array values.

Template variables that are arrays are traversed and the template is processed as many times as there are entries in the template variables array.

Picture of Michael Orji
  Performance   Level  

 

Example

<?php

   
include('../simplate.class.php');
   
   
/**
    * Defines an array for the users.
    * Each member of the array is an associative array with each user's data.
    * Typically this would be loaded from a database.
    */
   
$users = array(
        array(
'username'=>'KingSlayer', 'firstname'=>'Jamie', 'lastname'=>'Lanister', 'email'=>'kslayer@thelanisters.com', "location"=>'King\'s Landing'),
        array(
'username'=>'OmoSexy', 'firstname'=>'Omotola', 'lastname'=>'Ekeinde', 'email'=>'omosexy@naijamovies.net', 'location'=>'Nollywood'),
        array(
'username'=>'TheChosen', 'firstname'=>'panda', 'lastname'=>'kungfu', 'email'=>'panda@thefurious5.action', 'location'=>'Shifu Temple')
    );
   
   
/**
    * Loop through the users and creates a template for each one.
    * Because each user is an array with key/value pairs defined,
    * we made our template so each key matches a tag in the template,
    * allowing us to directly replace the values in the array.
    * We save each template in the $usersTemplates array.
    */
   
foreach ($users as $user)
    {
       
$row = new Simplate('templates/', 'users_list_item.tpl');
       
        foreach (
$user as $key => $value)
        {
           
$row->$key = $value;
        }
       
$usersTemplates[] = $row;
    }
   
   
/**
    * Merges all our users' templates into a single variable.
    * This will allow us to use it in the main template.
    */
   
$usersContents = Simplate::merge($usersTemplates);
   
   
/**
    * Defines the main template and sets the users' content.
    */
   
$usersList = new Simplate('templates/', 'users_list.tpl');
   
$usersList->users = $usersContents;
   
   
/**
    * Loads our layout template, settings its title and content.
    */
   
$layout = new Simplate('templates/', 'layout.tpl');
   
$layout->title = 'Registered Users';
   
$layout->content = $usersList->parse();
   
   
/**
    * Finally we can output our final page.
    */
   
echo $layout->parse();
   
?>


Screenshots (1)  
  • examples/images/koala.jpg
  Files folder image Files (12)  
File Role Description
Files folder imageexamples (4 files, 2 directories)
Plain text file simplate.class.php Class Main Class Source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:191
This week:0
All time:8,583
This week:41Up