Re[2]: Incorrect newline counters
Dmitry Koteroff <[email protected]>
| Newsgroups | gmane.comp.php.smarty.devel |
|---|---|
| Message-ID | <[email protected]> |
RA> Basically, the smarty templates are geared so the resulting
RA> output is the same as the template, and is a higher priority
RA> than the compiled template itself.
Aargh!
What do I see in _compile_foreach_start(), for example?
$output .= "if (isset(\$this->_foreach[$name])) unset(\$this->_foreach[$name]);\n";
$foreach_props = "\$this->_foreach[$name]";
WHAT IS THIS? Why \n? It breaks line number!
Now I write the patch which does TOTALLY remove ALL \n's from
compiled tag and after that adds needed "<?php\n?>" to correct
line counter. Piece of code:
+ $lT = substr_count($template_tags_sp[$i], "\n");
+ $lC = substr_count($compiled_tags[$i], "\n");
+ $compiled_tags[$i] = preg_replace('/\n+/s', ' ', $compiled_tags[$i]);
+ $compiled_tags[$i] .= str_repeat("<?php\n?".">", max($lT-$lC, 0));
I am too lazy to delete all \n's from hundreds of lines of code
manually - do it yourself if you want. Why have you inserted them
to the code at all?
--
Best regards,
Dmitry Koteroff.
--
Smarty Development Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Smarty_Compiler_diff.txt
(text/plain, 7 KB)
--- Distrib.orig/libs/Smarty_Compiler.class.php Tue Feb 17 19:55:24 2004
+++ Distrib/libs/Smarty_Compiler.class.php Mon May 31 03:23:38 2004
@@ -255,8 +255,9 @@
. "'"
, $source_content);
-
+
/* 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);
@@ -276,5 +277,5 @@
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 */
@@ -285,5 +286,5 @@
} 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]);
}
@@ -296,6 +297,13 @@
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 || 0) {
+ $text_blocks[$i] = preg_replace("/[\t ]+(?=\r?\n)|(?<=\n)[\t ]+/s", '', $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) {
@@ -309,23 +317,12 @@
/* 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];
+ $lT = substr_count($template_tags_sp[$i], "\n");
+ $lC = substr_count($compiled_tags[$i], "\n");
+ $compiled_tags[$i] = preg_replace('/\n+/s', ' ', $compiled_tags[$i]);
+ $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" )) {
@@ -338,5 +335,5 @@
// 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
@@ -355,6 +352,6 @@
// 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. */
@@ -369,5 +366,5 @@
}
$_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();
@@ -376,5 +373,5 @@
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;
}
@@ -412,5 +409,5 @@
$_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;