Re: templating, anyone?
| From: | sveta | Date: | Tue, 07 Sep 2004 11:25:52 +0000 |
| Subject: | Re: templating, anyone? | ||
| References: | 1 2 3 4 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-33266@lists.php.net to get a copy of this message | ||
Sebastian wrote:
- Template parsing is done natively by PHP -- no tricky dance moves, parsing overhead, or learning curve - Approximately 200 lines of code (most of which is wasted on formatting for readability)Dear PEARS! Discussion about templates gives me idea to tell about designs of my pseudo template "engine", named Template_Dummy :-) . /I wrote it after next discussion with designer about feature work for one project: _Me_: "Let's use Smarty: this engine contains much features and you should spend just 2 days to learn all of its features!" _Designer_: "May be I spend 2 days for learning features of PHP itself and we'll use <?=$variable?> and <?php easy_code ?> constructions? Do you see different with separating of code and design with constructions using Smarty templates: {$smarty_variable} and, for example, {section name=name1 loop=$loop1}<td>{$loop1[=name1 ]->base_output()}</td>{/section}?" _Note_: more part of articles devoted templates, contain next maxim: designer can't understand very difficult control structures like foreach or if. Interesting, how designers write JavaScript programs without these very difficult control structures? I think, power of templates is separating output variables from other (and other possibilities too :-) ). But this power can be a weakness from necessity to spend server resources for analizing templates./ If somebody has some reasons to use PHP itself as "template engine", [s]he should think about visibility of output variables. Simplest way to do it is encapsulating variables in class. *JUST *it do pseudo template "engine" Template_Dummy. These are few classes containing members, which hold custom variables intended to output and functions, return values of which intended to output too. StaticDummy is one of their contains only *static *members and functions. As is PHP4 version of StaticDummy, templates can looks like this (working same with PHP4 and PHP5 StaticDummy): <?php //only if you want to open template separetely from script if (version_compare(phpversion(), '5.0.0.', '<')) { require_once 'Template/StaticDummy4.php'; } else { require_once 'Template/StaticDummy5.php'; } if (!StaticDummy::issetDefault()) {
StaticDummy::setDefault(array('some' => 'output'));
}
//end only if you want to open template separetely from script
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Voting results</title>
</HEAD>
<BODY>
Total votes: <b><?=StaticDummy::__call('getTotal', StaticDummy::__get('votes'))?></b><br>
<table border="1">
<?php foreach (StaticDummy::__get('votes') as $key => $value) { ?>
<tr><td>For <b><?=$key?></b> vote <b><?=$value?></b> persons</td></tr>
<?php } ?>
</table>
</BODY>
</HTML>
Below I paste PHP5 version of StaticDummy:
class StaticDummy {
//{{{ members
private static $variables = array(); //contains StaticDummy inner variables private static $functions = array(); //contains StaticDummy inner functions //contains value which StaticDummy returns when no inner variable or function//named as called set
private static $default = '';//flag shows: set somebody $default or it has default value
private static $isset_default = false;//}}} //{{{ methods //{{{ /* void */ setDefault(/* mixed */ $default = '') //sets value which StaticDummy returns when no inner variable or function //named as called set public static function setDefault($default = '') {
self::$default = $default;} //}}} //{{{ /* mixed */ getDefault() //gets value which StaticDummy returns when no inner variable or function //named as called set public static function getDefault() {
return self::$default;} //}}} //{{{ /* boolean */ issetDefault() //returns true if somebody set StaticDummy::$default public static function issetDefault() {
return self::$isset_default;} //}}} //{{{ [mixed] create_function([string] $function_name, [string] $args, [string] $function_body) //creates StaticDummy::function_name($args) using create_function //returns result of create_function in success //returns false on error public static function create_function($function_name, $args, $function_body) {
if (!array_key_exists ($function_name, StaticDummy::$functions)) {
self::$functions[$function_name] = create_function($args, $function_body);
return self::$functions[$function_name];
} else {
return false;
}
}
//}}}
//{{{ [mixed] __get([string] $variable_name)
//returns value of StaticDummy::variable_name
//or StaticDummy::default if StaticDummy::variable_name not set
public static function __get($variable_name) {
if (array_key_exists($variable_name, self::$variables)) {
return self::$variables[$variable_name];
} else {
return self::$default;
}
}
//}}}
//{{{ [void] __set([string] $variable_name, [mixed] $value)
//creates StaticDummy::variable_name if not exists and
//sets $value to StaticDummy::variable_name
public static function __set($variable_name, $value) {
self::$variables[$variable_name] = $value;} //}}} //{{{ __call([string] $function_name, [mixed] $params) //calls StaticDummy::function_name($params) //returns StaticDummy::default on error public static function __call($function_name, $params) {
if (array_key_exists($function_name, self::$functions)) {
return call_user_func(self::$functions[$function_name], $params); //array(&$this, )
} else {
return self::$default;
}
}
//}}}
//}}}
}
Here:
http://www.microbecal.com/ftp_load/Dummy.zip
you can load zip with both PHP4 and PHP5 versions of StaticDummy and 2 examples.
Of course, StaticDummy is not pretend to be named "template engine" and was NOT writen for next massive promotion :-) . But if somebody like it, let me know and I will publish it.
Yours sincerely,
Sveta.
--
Best regards,
Sveta Smirnova
mailto:sveta@microbecal.com