Login   Register  
PHP Classes
elePHPant
Icontem

File: example_build.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Kalle Sommer Nielsen  >  Template Processor  >  example_build.php  >  Download  
File: example_build.php
Role: Example script
Content type: text/plain
Description: Example file - Using build mode
Class: Template Processor
Template processing engine
Author: By
Last change: Updated to match the latest class version
Date: 2007-01-18 09:18
Size: 1,740 bytes
 

Contents

Class file image Download
<?php
    
/**
     * Load class
     */
    
require_once("./template.class.php");

    
/**
     * Make object instance and set 'vartype' to 'php' (from v1.0.2)
     */
    
$temp = new Template(Array('vartype' => 'php'));

    
/**
     * This is some dummy data we're later gonna 
     * run though a foreach
     */
    
$data = Array(
            Array(
                
'Kalle'
                
17
                
), 
            Array(
                
'Joe'
                
29
                
)
            );

    
/**
     * Add some random markup that will appear as 
     * a header of some sort
     */
    
$temp->addcache('<div><h3>Top Programmers</h3><ul>');

    
/**
     * You may call $temp->clear_cache(true) to clear the
     * build cache before using it, but since we havn't in 
     * this example we don't
     */

    /**
     * Run the foreach
     */
    
foreach($data as $info)
    {
        
/**
         * Add some template cache into the 'build' cache
         */
        
$temp->addcache("<li>Name: \$name<br/>Age: \$age</li>"true);

        
/**
         * Replace variables in 'build' cache
          *
         *    Note. that we dont set the option to replace
         *    'phpvars' in the replace_var() function (<= v1.0.1 only)
         *
         */
        
$temp->replace_var('name'$info[0], falsetrue);
        
$temp->replace_var('age'$info[1], falsetrue);

        
/**
         * Build current cache, build() will clear the cache when its done
         */
        
$temp->build();
    }

    
/**
     * Add some random markup that will appear as 
     * a footer of some sort
     */
    
$temp->addcache('</ul></div>');

    
/**
     * Compile example
     */
    
$temp->compile();

    
/**
     * HTML Dump should be something like this:
     *
     *    <div>
     *        <h3>Top Programmers</h3>
     *        <ul>
     *            <li>
     *                Name: Kalle<br/>Age: 17
     *            </li>
     *            <li>
     *                Name: Joe<br/>Age: 29
     *            </li>
     *        </ul>
     *    </div>
     *
     */
?>