PHP Classes

File: examples/list_users.php

Recommend this page to a friend!
  Classes of Michael Orji   Simplate   examples/list_users.php   Download  
File: examples/list_users.php
Role: Example script
Content type: text/plain
Description: example usage
Class: Simplate
Template engine that replaces arrays of variables
Author: By
Last change:
Date: 10 years ago
Size: 1,865 bytes
 

Contents

Class file image Download
<?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();
   
?>