Re: [SMARTY-DEV] {strip}{/strip} unexpected results

From: Date: Thu, 17 Apr 2003 15:34:40 +0000
Subject: Re: [SMARTY-DEV] {strip}{/strip} unexpected results
References: 1 2  Groups: php.smarty.dev 
Request: Send a blank email to php-smarty-dev+get-1219@lists.php.net to get a copy of this message
Hmm... I personly don't find it an problem that the layout of HTML that Smarty does produce is not the same as the layout in the template, but others may disagree ;-). So I tried turning around the order in the function _compile_file(...). First strip and then compile the tags. The output display as expected. This however will make Smarty report the wrong line in case of an error during compiling since everything is on the same line. Greetings, Jan Rosier * Without tradition, art is a flock of sheep without a shepherd. Without innovation, it is a corpse. - Sir Winston Leonard Spencer Churchill * ----- Original Message ----- From: "Monte Ohrt" <monte@ispi.net> To: "Jan Rosier" <rosier@interstroom.nl> Cc: "Smarty Dev" <smarty-dev@lists.php.net> Sent: Thursday, April 17, 2003 3:28 PM Subject: Re: [SMARTY-DEV] {strip}{/strip} unexpected results > The compiler inserts extra newlines after '?>' tags in an attempt to > make the template output exactly like the template source. PHP eats > these newlines, so in many cases it is necessary to add a newline so > that the output doesn't get bunched together. > > Monte > > On Thu, 2003-04-17 at 08:07, Jan Rosier wrote: > > Hello all, > > > > I found that {strip}{/strip} does not always work as you might expect. I > > will try to explain this with an example > > > > In php: > > $smarty->assign('firstname', 'John'); > > $smarty->assign('lastname', 'Smith'); > > > > Template: > > {strip} > > <P> > > {$firstname}{if $lastname neq ''} {$lastname}{/if} > > </P> > > {/strip} > > {strip} > > <P> > > {$firstname} {$lastname} > > </P> > > {/strip} > > > > Output: > > <P>John Smith</P> > > <P>JohnSmith</P> > > > > The output of the first paragraph is as expected. The second one not. Here > > the space between John and Smith is stripped out > > > > After some digged in Smarty_Compiler.class.php v2.5.0 I found that the > > compiler inserts extra newlines, which interfere with the code that is use > > for strip (line 308 - 317) > > > > I changed line 396: return "<?php echo $_return; ?>\n"; > > to: return '<?php echo ' . $_return . '; ?>'; > > Now the output of the second paragraph is the same as the output of the > > first paragraph. > > > > Why does the Smarty compiler insert extra newlines in the compiled template? > > Does my change break anything else? If not, then it would be nice if this > > change was put in the distribution as well. > > > > Greetings > > Jan Rosier. > -- > Monte Ohrt <monte@ispi.net> > >

255a256,267 > /* Reformat data between 'strip' and '/strip' tags, removing spaces, tabs and newlines. */ > if (preg_match_all("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s", $template_source, $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++) { > $template_source = preg_replace("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s", > $this->quote_replace($strip_tags_modified[$i]), > $template_source, 1); > } > } > 261c273 < --- > 308,318d319 < /* Reformat data between 'strip' and '/strip' tags, removing spaces, tabs and newlines. */ < if (preg_match_all("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s", $template_compiled, $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++) < $template_compiled = preg_replace("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s", < $this->quote_replace($strip_tags_modified[$i]), < $template_compiled, 1); < } <
« previous php.smarty.dev (#1219) next »