Re: Incorrect newline counters

Dmitry Koteroff <[email protected]>
Newsgroups gmane.comp.php.smarty.devel
Message-ID <[email protected]>
Complex  patch  attached.  I  have  not tested it completely, but
seems everything works fine.

I  had  to  change  {strip} replacement method - we cannot delete
\n's, we must replace it with "<?php\n?>", and this is impossible
inside  <?...?> blocks. Now {strip} changes only text blocks (not
tag compiling results) -

if ($this->_strip_depth) {
   $text_blocks[$i] = preg_replace("![\t ]+$|^[\t ]+!m", '', $text_blocks[$i]);
   $text_blocks[$i] = preg_replace("/\r?\n/s", "<?php\n?".">", $text_blocks[$i]);
}

-- 
Best regards,
  Dmitry Koteroff.

DK> Hello.

DK> Smarty do not preserve line numbers while compiling the template.
DK> When an error occurs, PHP shows line number in compiled file, and
DK> it does not correspond line number in the source template.

DK> Example. If I have the following template:

DK> 1 {t_block name="Title"}...{/t_block}
DK> 2 {t_block name="Author"}...{/t_block}
DK> 3 {t_block name="Text"}
DK> 4 {strip}
DK> 5 ss
DK> 6 ddd
DK> 7 {/strip}
DK> 8 {$a}

DK> and  $a  is undefined, I get notice not in line 8, but in other.
DK> Resulting template contains:
DK> a) superfluous  \n's  - in the headder comments /* compiled from
DK>    ...*/, and after plugins require_once's;
DK> b) insufficient spaces - {strip} processed incorrectly, it glues
DK>    its body, replacing \n by '', but not by <?php\n?>

DK> What do you thing about all that? If I write the patch which will
DK> correct line numbers, will you include it in production version?

-- 
Smarty Development Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
FileComparisonDiffReport.txt (text/plain, 7.5 KB)
--- C:\TMP\s\Smarty_Compiler.class.php	Tue Feb 17 15:55:24 2004 UTC
+++ Z:\home\orphus\www\WEB-INF\lib\Smarty\Distrib\libs\Smarty_Compiler.class.php	Sat May 29 22:10:06 2004 UTC
@@ -258,6 +258,7 @@
         /* Gather all template tags. */
         preg_match_all("!{$ldq}\s*(.*?)\s*{$rdq}!s", $source_content, $_match);
         $template_tags = $_match[1];
+        $template_tags_sp = $_match[0];
         /* Split content by template tags to obtain non-template content. */
         $text_blocks = preg_split("!{$ldq}.*?{$rdq}!s", $source_content);
 
@@ -275,7 +276,7 @@
                 for ($curr_sp = 0, $for_max2 = count($sp_match[1]); $curr_sp < $for_max2; $curr_sp++) {
                     if ($this->php_handling == SMARTY_PHP_PASSTHRU) {
                         /* echo php contents */
-                        $text_blocks[$curr_tb] = str_replace('%%%SMARTYSP'.$curr_sp.'%%%', '<?php echo \''.str_replace("'", "\'", $sp_match[1][$curr_sp]).'\'; ?>'."\n", $text_blocks[$curr_tb]);
+                        $text_blocks[$curr_tb] = str_replace('%%%SMARTYSP'.$curr_sp.'%%%', '<?php echo \''.str_replace("'", "\'", $sp_match[1][$curr_sp]).'\'; ?>'."", $text_blocks[$curr_tb]);
                     } else if ($this->php_handling == SMARTY_PHP_QUOTE) {
                         /* quote php tags */
                         $text_blocks[$curr_tb] = str_replace('%%%SMARTYSP'.$curr_sp.'%%%', htmlspecialchars($sp_match[1][$curr_sp]), $text_blocks[$curr_tb]);
@@ -284,7 +285,7 @@
                         $text_blocks[$curr_tb] = str_replace('%%%SMARTYSP'.$curr_sp.'%%%', '', $text_blocks[$curr_tb]);
                     } else {
                         /* SMARTY_PHP_ALLOW, but echo non php starting tags */
-                        $sp_match[1][$curr_sp] = preg_replace('%(<\?(?!php|=|$))%i', '<?php echo \'\\1\'?>'."\n", $sp_match[1][$curr_sp]);
+                        $sp_match[1][$curr_sp] = preg_replace('%(<\?(?!php|=|$))%i', '<?php echo \'\\1\'?>'."", $sp_match[1][$curr_sp]);
                         $text_blocks[$curr_tb] = str_replace('%%%SMARTYSP'.$curr_sp.'%%%', $sp_match[1][$curr_sp], $text_blocks[$curr_tb]);
                     }
                 }
@@ -295,8 +296,15 @@
         $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");
+            if ($this->_strip_depth) {
+                $text_blocks[$i] = preg_replace("![\t ]+$|^[\t ]+!m", '', $text_blocks[$i]);
+                $text_blocks[$i] = preg_replace("/\r?\n/s", "<?php\n?".">", $text_blocks[$i]);
+            }
             $compiled_tags[] = $this->_compile_tag($template_tags[$i]);
             $this->_current_line_no += substr_count($template_tags[$i], "\n");
+            if ($template_tags[$i] == "strip" || $template_tags[$i] == "/strip") {
+            	$compiled_tags[count($compiled_tags)-1] = "";
+            }
         }
         if (count($this->_tag_stack)>0) {
             list($_open_tag, $_line_no) = end($this->_tag_stack);
@@ -308,25 +316,13 @@
 
         /* 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]);
-            }
+            $lT = substr_count($template_tags_sp[$i], "\n");
+            $lC = substr_count($compiled_tags[$i], "\n");
+			$compiled_tags[$i] .= str_repeat("<?php\n?".">", max($lT-$lC, 0));
             $compiled_content .= $text_blocks[$i].$compiled_tags[$i];
         }
         $compiled_content .= $text_blocks[$i];
 
-        /* Reformat data between 'strip' and '/strip' tags, removing spaces, tabs and newlines. */
-        if (preg_match_all("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s", $compiled_content, $_match)) {
-            $strip_tags = $_match[0];
-            $strip_tags_modified = preg_replace("!{$ldq}/?strip{$rdq}|[\t ]+$|^[\t ]+!m", '', $strip_tags);
-            $strip_tags_modified = preg_replace('![\r\n]+!m', '', $strip_tags_modified);
-            for ($i = 0, $for_max = count($strip_tags); $i < $for_max; $i++)
-                $compiled_content = preg_replace("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s",
-                                                  $this->_quote_replace($strip_tags_modified[$i]),
-                                                  $compiled_content, 1);
-        }
-
         // remove \n from the end of the file, if any
         if (($_len=strlen($compiled_content)) && ($compiled_content{$_len - 1} == "\n" )) {
             $compiled_content = substr($compiled_content, 0, -1);
@@ -337,7 +333,7 @@
         }
 
         // remove unnecessary close/open tags
-        $compiled_content = preg_replace('!\?>\n?<\?php!', '', $compiled_content);
+        $compiled_content = preg_replace('!\?'.'><\?php!', '', $compiled_content);
 
         // run compiled template through postfilter functions
         if (count($this->_plugins['postfilter']) > 0) {
@@ -354,8 +350,8 @@
         }
 
         // put header at the top of the compiled template
-        $template_header = "<?php /* Smarty version ".$this->_version.", created on ".strftime("%Y-%m-%d %H:%M:%S")."\n";
-        $template_header .= "         compiled from ".strtr(urlencode($resource_name), array('%2F'=>'/', '%3A'=>':'))." */ ?>\n";
+        $template_header = "<?php /* Smarty version ".$this->_version.", created on ".strftime("%Y-%m-%d %H:%M:%S")."";
+        $template_header .= "         compiled from ".strtr(urlencode($resource_name), array('%2F'=>'/', '%3A'=>':'))." */ ?>";
 
         /* Emit code to load needed plugins. */
         $this->_plugins_code = '';
@@ -368,14 +364,14 @@
                 }
             }
             $_plugins_params .= '))';
-            $plugins_code = "<?php require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.load_plugins.php');\nsmarty_core_load_plugins($_plugins_params, \$this); ?>\n";
+            $plugins_code = "<?php require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.load_plugins.php'); smarty_core_load_plugins($_plugins_params, \$this); ?>";
             $template_header .= $plugins_code;
             $this->_plugin_info = array();
             $this->_plugins_code = $plugins_code;
         }
 
         if ($this->_init_smarty_vars) {
-            $template_header .= "<?php require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.assign_smarty_interface.php');\nsmarty_core_assign_smarty_interface(null, \$this); ?>\n";
+            $template_header .= "<?php require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.assign_smarty_interface.php'); smarty_core_assign_smarty_interface(null, \$this); ?>";
             $this->_init_smarty_vars = false;
         }
 
@@ -411,7 +407,7 @@
             /* tag name is a variable or object */
             $_return = $this->_parse_var_props($tag_command . $tag_modifier, $this->_parse_attrs($tag_args));
             if(isset($_tag_attrs['assign'])) {
-                return "<?php \$this->assign('" . $this->_dequote($_tag_attrs['assign']) . "', $_return ); ?>\n";
+                return "<?php \$this->assign('" . $this->_dequote($_tag_attrs['assign']) . "', $_return ); ?>";
             } else {
                 return "<?php echo $_return; ?>" . $this->_additional_newline;
             }
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.