Re: [SMARTY-DEV] Optimizing performance for mass-includes + Code

From: Date: Mon, 23 Feb 2004 15:14:12 +0000
Subject: Re: [SMARTY-DEV] Optimizing performance for mass-includes + Code
References: 1  Groups: php.smarty.dev 
Request: Send a blank email to php-smarty-dev+get-2132@lists.php.net to get a copy of this message
thoughts on potential problems: * variable scope on included templates * determining when a recompile is necessary The easiest solution to the php include() performance is to turn on template caching, then you only load one cache file. Monte On Sun, 2004-02-22 at 01:52, Thomas Bley wrote: > 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

« previous php.smarty.dev (#2132) next »