Re: [SMARTY-DEV] compile-block-functions (strip)

From: Date: Thu, 26 Jun 2003 23:23:07 +0000
Subject: Re: [SMARTY-DEV] compile-block-functions (strip)
References: 1  Groups: php.smarty.dev 
Request: Send a blank email to php-smarty-dev+get-1703@lists.php.net to get a copy of this message
Hello again... I had a quick look at 'Smarty_Compiler.class.php' and found a way to make compile-block-functions a little bit easier (but not perfect): existing code (line311-329) : --------------------------------------------- /* Compile the template tags into PHP code. */ $compiled_tags = array(); for ($i = 0, $for_max = count($template_tags); $i < $for_max; $i++){ $this->_current_line_no += substr_count($text_blocks[$i], "\n"); $compiled_tags[] = $this->_compile_tag($template_tags[$i]); $this->_current_line_no += substr_count($template_tags[$i],"\n"); } $compiled_content = ''; /* Interleave the compiled contents and text blocks to get the final result. */ for ($i = 0, $for_max = count($compiled_tags); $i < $for_max; $i++){ if ($compiled_tags[$i] == '') { // tag result empty, remove first newline from following text block $text_blocks[$i+1] = preg_replace('!^(\r\n|\r|\n)!', '',$text_blocks[$i+1]); } $compiled_content .= $text_blocks[$i].$compiled_tags[$i]; } $compiled_content .= $text_blocks[$i]; --------------------------------------------- My suggestion: - combine the two for-loops - make a reference from $compiled_content to $this->_compiled_content (a new local class attribute in smarty-class) - if you like you can unset the new '$this->_compiled_content' attribute afterwards Now all compiler-plugin-functions can access the (so far) compiled content (using $smarty->_compiled_content). Because of the reference its done without preformance penalties. For compiler-block-plugins you have to: - register a {foo} compiler-function which insert a magic-tag - register a {/foo} compiler-function. The content after the *last* magic-tag in smarty->_compiled_content is the content within this {foo}...{/foo} block - it also works for nested blocks the new code: --------------------------------------------- /* Compile the template tags into PHP code */ /* and Interleave the compiled contents and text blocks to get the final result. */ $compiled_tags = array(); $compiled_content = ''; $this->_compiled_content =& $compiled_content; for ($i = 0, $for_max = count($template_tags); $i < $for_max; $i++){ $this->_current_line_no += substr_count($text_blocks[$i], "\n"); $compiled_content .= $text_blocks[$i]; $compiled_tags[$i] = $this->_compile_tag($template_tags[$i]); if ($compiled_tags[$i] == '') { // tag result empty, remove first newline from following text block $text_blocks[$i+1] = preg_replace('!^(\r\n|\r|\n)!', '',$text_blocks[$i+1]); } $compiled_content .= $compiled_tags[$i]; $this->_current_line_no += substr_count($template_tags[$i],"\n"); } $compiled_content .= $text_blocks[$i]; unset($this->_compiled_content); --------------------------------------------- I hope you like it... Nagger

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