Bugfixes and some other stuff...

From: Date: Sun, 14 Jul 2002 02:00:10 +0000
Subject: Bugfixes and some other stuff...
Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-5285@lists.php.net to get a copy of this message
Warning: Failed opening '\N117\N1172603085\index.tpl.php' for inclusion (include_path='.;e:/httpd/php/pear') in path\to\Smarty-2.2.0-CVS\Smarty.class.php on line 656 ... comes from missing templates_c directory in _get_auto_filename(). _get_include_path() already returns FALSE in case it couldn't find the file, so I suppose lines 1446 ff. in Smarty.class.php should be changed to something like this: // auto_base not found, try include_path if(!$this->_get_include_path($auto_base,$_include_path)) { //still not found, create it mkdir($auto_base, 0700); $res = $auto_base . DIR_SEP; } else { $res = $_include_path . DIR_SEP; } *** Fatal error: Call to undefined function: () in path\to\Smarty-2.2.0-CVS\Smarty.class.php on line 1004 ... is a Windows bug: debug.tpl is prepended with SMARTY_DIR in class constructor, but absolute paths need a resource type. Line 216 should look like this: $this->debug_tpl = 'file:'.SMARTY_DIR.'debug.tpl'; *** Some performance hints... Double-quoted strings containing variables: Writing something like: $arg_list[] = "'$arg_name' => $arg_value"; instead of separating string and variables: $arg_list[] = "'".$arg_name."' => ".$arg_value; is 25% slower. SWITCH constructs using literal strings: Instead of writing: switch ($tag_command) { case 'include': return $this->_compile_include_tag($tag_args); case 'include_php': return $this->_compile_include_php_tag($tag_args); ... why not using PHP's fast hashing algorithm using a lookup table: $_TAGS = array( 'include' => 1, 'include_php' => 2, 'if' => 3, ... ); switch ($_TAGS[$tag_command]) { case 1: return $this->_compile_include_tag($tag_args); case 2: return $this->_compile_include_php_tag($tag_args); case 3: return $this->_compile_if_tag($tag_args); ... With only two test CASEs, the code is 5% faster (almost nothing), but with 4 CASEs, the code executes about 30% faster. *** From the TODO list: Idea to correctly capture nested php tag syntax in templates: <?php echo "<?php exit(); ?>"; ?> I use some code to strip all literal strings (both double- and single-quoted) from a string containing executable logic: First I replace all literal strings in non-escaped quotes with control codes 0x01 and 0x02, resp. Then I perform all my work on the 'cleaned' string, and last I inject the stripped literal strings again. function save_literal( &$expr ) { $stack = array(); preg_match_all( "/(?<!\\\\)\"(?:(?<=\\\\)(?:\\\\\\\\)+\"|(?<=(?<!\\\\)\\\\)\"|[^\"])*\"/ ", $expr, $reg ); $expr = str_replace( $stack[] = $reg[0], "\x01", $expr ); preg_match_all( "/(?<!\\\\)'(?:(?<=\\\\)(?:\\\\\\\\)+'|(?<=(?<!\\\\)\\\\)'|[^'])*'/", $expr, $reg ); $expr = str_replace( $stack[] = $reg[0], "\x02", $expr ); return $stack; } function restore_literal( $expr, $stack ) { $expr = vsprintf( str_replace("\x02", "%s", $expr), array_pop($stack) ); return vsprintf( str_replace("\x01", "%s", $expr), array_pop($stack) ); } //assume $expr = "<?php echo \"<?php exit(); ?>\"; ?>"; //modify $expr, save the returned stack $stack = save_literal( $expr ); //do something with $expr ... //inject saved literal strings in (modified) $expr again $expr = restore_literal( $expr, $stack ); *** Ok, enough said for today .SMK.

« previous php.smarty.general (#5285) next »