Optimizing performance for mass-includes + Code

Thomas Bley <[email protected]>
Newsgroups gmane.comp.php.smarty.devel
Message-ID <[email protected]>
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

-- 
Smarty Development Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.