Optimizing performance for mass-includes + Code
| From: | Thomas Bley | Date: | Sun, 22 Feb 2004 07:52:05 +0000 |
| Subject: | Optimizing performance for mass-includes + Code | ||
| Groups: | php.smarty.dev | ||
| Request: | Send a blank email to php-smarty-dev+get-2128@lists.php.net to get a copy of this message | ||
Hi,
I found out that mass including is very slow using php in general, e.g.
{foreach from=$temp_table.fields key=curr_id item=item}
{include file="asset_functions.tpl" section="item_fields"}
{/foreach}
{foreach from=$temp_table.fields key=curr_id item=item}
{include file="asset_functions.tpl" section="item_data"}
{/foreach}
this is much slower than including the code directly between the foreach statements.
My Solution: (based on smarty 2.6.2)
-- code can be copied without copyright hint
(increases performance with complexity of n depending on the number of n foreach iterations)
I create a prefilter :
The filter takes a look at the template and replaces all includes with the code directly (if the replaced code also has includes it works recursively)
$smarty->register_prefilter("modify_includes");
function replace_file($match) {
global $smarty;
// get the code from the included file
if (isset($match[1]) and $match[1]!="") $result = modify_includes($smarty->_read_file($smarty->template_dir."/".$match[1]),$smarty);
// map the assigned variables (using include file="" var1="value1") to assign
if (isset($match[2]) and $match[2]!="") {
while (preg_match("|(.*?)=\"(.*?)\"|i",$match[2],$var)) {
$match[2] = str_replace($var[0],"",$match[2]);
if (count($var)==3) {
$result = "{assign var=\"".trim($var[1])."\" value=\"".$var[2]."\"}\n".$result;
}
}
}
return $result;
}
function modify_includes($tpl_source, &$smarty) {
$tpl_source = preg_replace_callback("|{include file=\"(.*?)\"(.*?)}|im","replace_file",$tpl_source);
return $tpl_source;
}
bye
tom