Re: [SMARTY-DEV] Newlines

From: Date: Thu, 04 Apr 2002 17:39:34 +0000
Subject: Re: [SMARTY-DEV] Newlines
References: 1  Groups: php.smarty.dev 
Request: Send a blank email to php-smarty-dev+get-23@lists.php.net to get a copy of this message
Ah yes, the problem extends from the fact that PHP eats carriage returns immediately after a php close tag. We cannot simply remove this \n in Smarty, it will cause incorrect formatting. I have attached a revised version of trimwhitespace, it is actually more efficient since one regex now takes care of both leading spaces and empty lines. This is accomplished with a negative match look behind, which PCRE thankfully supports ;-) I'm also trying to figure out a way to selectively disable trimwhitespace (besides unregistering the function.) For instance, if your template is an e-mail message, you don't want to trim it! My first thought was to look for {* trimwhitespace off *} in the template, but in a post filter that would already be removed. We could change Smarty so that Smarty comments are kept in the source, so {* foo *} would be come <?php /* foo */ ?> in the compiled template. Any thoughts on this? Actually I don't think this list supports attachments, so I'll paste the source here: <?php /* * Smarty plugin * -------------------------------------------------------------
 * File:     postfilter.trimwhitespace.php
 * Type:     postfilter
 * Name:     trimwhitespace
* Version: 1.1
 * Date:     April 4, 2002
* Purpose: trim leading white space and blank lines from
 *           template source after it gets interpreted, cleaning
 *           up code and saving bandwidth. Does not affect
 *           <PRE></PRE> and <SCRIPT></SCRIPT> blocks.
* Install: Drop into the plugin directory. * Author: Monte Ohrt <monte@ispi.net> * Changes: 1.1 fixed bug with carriage returns after php tag * ------------------------------------------------------------- */ function smarty_postfilter_trimwhitespace($source, &$smarty) {
    // Pull out the script blocks
    preg_match_all("!<script[^>]+>.*?</script>!is", $source, $match);
    $_script_blocks = $match[0];
    $source = preg_replace("!<script[^>]+>.*?</script>!is",
    '@@@SMARTY:TRIM:SCRIPT@@@', $source);
    // Pull out the pre blocks
    preg_match_all("!<pre>.*?</pre>!is", $source, $match);
    $_pre_blocks = $match[0];
    $source = preg_replace("!<pre>.*?</pre>!is",
    '@@@SMARTY:TRIM:PRE@@@', $source);
// remove all leading spaces, tabs and carriage returns NOT // preceeded by a php close tag.
      	$source = preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source);
// replace script blocks foreach($_script_blocks as $curr_block) { $source = preg_replace("!@@@SMARTY:TRIM:SCRIPT@@@!",$curr_block,$source,1); } // replace pre blocks foreach($_pre_blocks as $curr_block) { $source = preg_replace("!@@@SMARTY:TRIM:PRE@@@!",$curr_block,$source,1); } return $source; } ?> Robert Amos wrote:
From an earlier email that Jimmy sent to the list:
            $this->_parse_vars_props($tag_command);
            return "<?php echo $tag_command[0]; ?>"; <<<<<<<< I REMOVED
the \n at the end
        }
there is a newline placed after the variable, which isnt normally affected in displaying the page, but combined with the trimwhitespace postfilter it removes the space from the start of the next line. -bok On 4/4/02 12:16 AM, Monte Ohrt wrote the following:
Hmm, trimwhitespace should only trim space out from the beginning and ends of lines, from from between something like "{$var} text" Is that not the case? Jason Sweat wrote:
I did some further testing because I encountered the same problem. It has to do with postfilter.trimwhitespace.php No postfilter, no problem. Or as another temporary solution if you want to keep the postfilter, hard code a non-breaking space in between the var and the text (i.e. {$var} &nbsp; text). I am not sufficiently adept with regexp to solve the problem, but at least it is better identified. Jason ----- Original Message ----- From: "Monte Ohrt" <monte@ispi.net> To: "Jimmy Harlindong" <jimmy@harlindong.com> Cc: <smarty-dev@lists.php.net> Sent: Tuesday, April 02, 2002 9:11 AM Subject: Re: [SMARTY-DEV] Newlines
I cannot reproduce this problem. Here is my simple test: index.tpl --------- begin TESTING {$var} test TESTING2 {$var} test done OUTPUT ------ begin TESTING foo test TESTING2 foo test done This is exactly the output I would expect. Something else must be affecting the output. Are you putting strip around this, or do you have pre/post filters that may affect it? strip would take carriage returns off of the beginning and ends of lines, but it should not affect "{$var} text" if they are together. Jimmy Harlindong wrote:
When I have $var = "abc" and inside the template: {$var} text the output will be "abctext" How do I make it into "abc text" (with space there) On another case: get "abctext" only when I write {$var}text in the template? This seems to work: Smarty_compiler.class.php inside _compile_tag() function:
      /* If the tag name matches a variable or section property
definition,
         we simply process it. */
      if
(preg_match('!^\$\w+(?>(\[(\d+|\w+(\.\w+)?)\])|((\.|->)\w+))*(?>\|@?\w+( :(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command) || // if a variable
          preg_match('!^#(\w+)#(?>\|@?\w+(:(?>' . $qstr_regexp .
'|[^|]+))*)*$!', $tag_command)     ||  // or a configuration variable
          preg_match('!^%\w+\.\w+%(?>\|@?\w+(:(?>' . $qstr_regexp .
'|[^|]+))*)*$!', $tag_command)) {    // or a section property
          settype($tag_command, 'array');
          $this->_parse_vars_props($tag_command);
          return "<?php echo $tag_command[0]; ?>"; <<<<<<<< I REMOVED
the \n at the end
      }
Any comments? Or perhaps there is a workaround to this problem?
-- Monte Ohrt <monte@ispi.net> Director of Technology, ispi Inc. -- Smarty Development Mailing List (http://smarty.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
-- Monte Ohrt <monte@ispi.net> Director of Technology, ispi Inc.

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