PHP Classes

File: template.commands.php

Recommend this page to a friend!
  Classes of Matthew Knowlton   ExTemplate   template.commands.php   Download  
File: template.commands.php
Role: Auxiliary script
Content type: text/plain
Description: A few useful commands
Class: ExTemplate
Extensible template engine with callable functions
Author: By
Last change:
Date: 9 years ago
Size: 2,449 bytes
 

Contents

Class file image Download
<?php
require_once('template.class.php');

function
addTemplateCommands(template &$template){

   
$template->add_command('assign', function ($params) use ($template){
       
$template->macros[$params[0]] = $params[1];
        return
'';
    },
true);

   
$template->add_command('isset', function($params) use ($template){
        return (bool)isset(
$template->macros[$params[0]]);
    },
true);

   
$template->add_command('empty', function($params) use ($template){
        return empty(
$template->macros[$params[0]]);
    },
true);

   
$template->add_command('count', function($params) use ($template){
        return
count($template->macros[$params[0]]);
    },
true);

   
$template->add_command('equate', function($params) use ($template){
       
$equation = implode('',$params);
        return eval(
'return (float)('.$equation.');');
    });

   
$compare = function ($params) use ($template){
        if(
$params[1] == '==') return (bool)($params[0] == $params[2]);
        if(
$params[1] == '!=') return (bool)($params[0] != $params[2]);
        if(
$params[1] == '>') return (bool)($params[0] > $params[2]);
        if(
$params[1] == '<') return (bool)($params[0] < $params[2]);
        if(
$params[1] == '>=') return (bool)($params[0] >= $params[2]);
        if(
$params[1] == '<=') return (bool)($params[0] <= $params[2]);

        if(
$params[1] == '+') return (int)($params[0] + $params[2]);
        if(
$params[1] == '-') return (int)($params[0] - $params[2]);
        if(
$params[1] == '/') return (int)($params[0] / $params[2]);
        if(
$params[1] == '*') return (int)($params[0] * $params[2]);
        if(
$params[1] == '%') return (int)($params[0] % $params[2]);

        if(
$params[1] == '.') return $params[0] . $params[2];
        return (bool)@(
$params[3]);
    };

   
$template->add_command('compare', $compare, true);
   
$template->add_command('', $compare, true);

   
$template->add_command('render', function($params) use ($template){
        return
$template->render($template->load($params[0]));
    });

   
$inc = function ($params) use ($template){
        return
$template->macros[$params[0]]++;
    };

   
$template->add_command('inc', $inc);
   
$template->add_command('++', $inc);

   
$template->add_command('print_r', function ($params) use ($template){
        return
'<pre>'.print_r($template->macros[$params[0]],true).'</pre>';
    });

}

?>