PHP Classes

Simplate: Template engine that replaces arrays of variables

Recommend this page to a friend!
  Info   View files Example   Screenshots Screenshots   View files View files (11)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 190 This week: 1All time: 8,588 This week: 560Up
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  
Name: Michael Orji <contact>
Classes: 2 packages by
Country: Nigeria Nigeria
Age: ???
All time rank: 346525 in Nigeria Nigeria
Week rank: 416 Up8 in Nigeria Nigeria Up

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  
  • examples/images/koala.jpg
  Files folder image Files  
File Role Description
Files folder imageexamples (4 files, 2 directories)
Plain text file simplate.class.php Class Main Class Source

  Files folder image Files  /  examples  
File Role Description
Files folder imageimages (1 file)
Files folder imagetemplates (5 files)
  Accessible without login Plain text file list_users.php Example example usage
  Accessible without login Plain text file styles.css Data styling for example
  Accessible without login Plain text file user.php Example example usage
  Accessible without login Plain text file user_loop.php Example example usage

  Files folder image Files  /  examples  /  images  
File Role Description
  Accessible without login Image file koala.jpeg Icon image file

  Files folder image Files  /  examples  /  templates  
File Role Description
  Accessible without login Plain text file layout.tpl Data template file
  Accessible without login Plain text file users_list.tpl Data template file
  Accessible without login Plain text file users_list_item.tpl Data template file
  Accessible without login Plain text file user_profile.tpl Data template file
  Accessible without login Plain text file user_profile_loop.tpl Data template file

 Version Control Unique User Downloads Download Rankings  
 0%
Total:190
This week:1
All time:8,588
This week:560Up