mrhappiness
03-15-2005, 11:23 AM
Hi there
I'm developing my own template engine atm.
A template file being called in my script get's converted to php-code. This code is used as long as the underlying template didn't get changed.
If the underlying template got changed it's "compiled" once again.
This way I don't have to parse the template over and over again but parse it only once and reuse the produced php-code.
Please have a look at the code being produced and tell me what you think about it. How can the code be optimised t make it somewhat faster?
tia
template:<h1>{headline capitalize default "Welcome"}</h1>
<p>Welcome!<br />
I proudly present to you all your users including their rights.</p>
{loop users}
{if _first}<ul>{endif}
<li>user: {username}
{loop rights}
{if _first}<ul>{endif}
<li>{right}</li>
{if _last}</ul>{endif}
{endloop rights}</li>
{if _last}</ul>{endif}
{endloop users}produced php:<?php
/*
Template compiled using ExT 2.0.0.0
Compilation date: Tue, 15 Mar 2005 16:19:18 +0100
Compilation time: 112.5 ms
Template: user_rights.tpl
*/
/* Load needed modifiers */
require_once $this->_folders['extension_dir'].'modifiers/modifier.capitalize.php';
/* Compiled loops */
function ext_loop____user_rights_tplusers_1($content, &$this) {
$result = '';
if (is_array($content))
foreach ($content as $data) {
if (!isset($data['username'])) $data['username'] = NULL;
if (!isset($data['rights'])) $data['rights'] = NULL;
$position = isset($position) ? ++$position : 1;
$count = isset($count) ? $count : count($content);
$first = $position == 1;
$last = $position == $count;
$result .= '
'.(((isset($first) ? $first : NULL)) ? '<ul>' : '').'
<li>user: '.$data['username'].'
'.ext_loop____user_rights_tplusers_1rights_1($data['rights'], $this).'</li>
'.(((isset($last) ? $last : NULL)) ? '</ul>' : '').'
';
}
return $result;
}
function ext_loop____user_rights_tplusers_1rights_1($content, &$this) {
$result = '';
if (is_array($content))
foreach ($content as $data) {
if (!isset($data['right'])) $data['right'] = NULL;
$position = isset($position) ? ++$position : 1;
$count = isset($count) ? $count : count($content);
$first = $position == 1;
$last = $position == $count;
$result .= '
'.(((isset($first) ? $first : NULL)) ? '<ul>' : '').'
<li>'.$data['right'].'</li>
'.(((isset($last) ? $last : NULL)) ? '</ul>' : '').'
';
}
return $result;
}
/* template function */
function ext____user_rights_tpl(&$this, &$data) {
error_reporting(E_ALL);
if (!is_array($data)) $data = array();
if (!isset($data['headline'])) $data['headline'] = NULL;
if (!isset($data['users'])) $data['users'] = NULL;
if (!isset($data['default'])) $data['default'] = NULL;
$result = '<h1>'.ext_compile_modifier_capitalize($data['headline'], array(0 => $data['default'], 1 => 'Welcome')).'</h1>
<p>Welcome!<br />
I proudly present to you all your users including their rights.</p>
'.ext_loop____user_rights_tplusers_1($data['users'], $this);
return $result;} ?>$data contains all vars assigned to the template
inside loops it contains the vars assigned to this loop
$this is a refernce to the template class instance used in the script
I'm developing my own template engine atm.
A template file being called in my script get's converted to php-code. This code is used as long as the underlying template didn't get changed.
If the underlying template got changed it's "compiled" once again.
This way I don't have to parse the template over and over again but parse it only once and reuse the produced php-code.
Please have a look at the code being produced and tell me what you think about it. How can the code be optimised t make it somewhat faster?
tia
template:<h1>{headline capitalize default "Welcome"}</h1>
<p>Welcome!<br />
I proudly present to you all your users including their rights.</p>
{loop users}
{if _first}<ul>{endif}
<li>user: {username}
{loop rights}
{if _first}<ul>{endif}
<li>{right}</li>
{if _last}</ul>{endif}
{endloop rights}</li>
{if _last}</ul>{endif}
{endloop users}produced php:<?php
/*
Template compiled using ExT 2.0.0.0
Compilation date: Tue, 15 Mar 2005 16:19:18 +0100
Compilation time: 112.5 ms
Template: user_rights.tpl
*/
/* Load needed modifiers */
require_once $this->_folders['extension_dir'].'modifiers/modifier.capitalize.php';
/* Compiled loops */
function ext_loop____user_rights_tplusers_1($content, &$this) {
$result = '';
if (is_array($content))
foreach ($content as $data) {
if (!isset($data['username'])) $data['username'] = NULL;
if (!isset($data['rights'])) $data['rights'] = NULL;
$position = isset($position) ? ++$position : 1;
$count = isset($count) ? $count : count($content);
$first = $position == 1;
$last = $position == $count;
$result .= '
'.(((isset($first) ? $first : NULL)) ? '<ul>' : '').'
<li>user: '.$data['username'].'
'.ext_loop____user_rights_tplusers_1rights_1($data['rights'], $this).'</li>
'.(((isset($last) ? $last : NULL)) ? '</ul>' : '').'
';
}
return $result;
}
function ext_loop____user_rights_tplusers_1rights_1($content, &$this) {
$result = '';
if (is_array($content))
foreach ($content as $data) {
if (!isset($data['right'])) $data['right'] = NULL;
$position = isset($position) ? ++$position : 1;
$count = isset($count) ? $count : count($content);
$first = $position == 1;
$last = $position == $count;
$result .= '
'.(((isset($first) ? $first : NULL)) ? '<ul>' : '').'
<li>'.$data['right'].'</li>
'.(((isset($last) ? $last : NULL)) ? '</ul>' : '').'
';
}
return $result;
}
/* template function */
function ext____user_rights_tpl(&$this, &$data) {
error_reporting(E_ALL);
if (!is_array($data)) $data = array();
if (!isset($data['headline'])) $data['headline'] = NULL;
if (!isset($data['users'])) $data['users'] = NULL;
if (!isset($data['default'])) $data['default'] = NULL;
$result = '<h1>'.ext_compile_modifier_capitalize($data['headline'], array(0 => $data['default'], 1 => 'Welcome')).'</h1>
<p>Welcome!<br />
I proudly present to you all your users including their rights.</p>
'.ext_loop____user_rights_tplusers_1($data['users'], $this);
return $result;} ?>$data contains all vars assigned to the template
inside loops it contains the vars assigned to this loop
$this is a refernce to the template class instance used in the script