[prev in list] [next in list] [prev in thread] [next in thread] 

List:       smarty-dev
Subject:    Re: [SMARTY-DEV] compile-block-functions (strip)
From:       "Nagger" <nagger () gmx ! de>
Date:       2003-06-26 23:23:07
[Download RAW message or body]

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


-- 
Smarty Development Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic