3 * Template Lite plugin converted from Smarty
9 * Smarty trimwhitespace outputfilter plugin
11 * File: outputfilter.trimwhitespace.php<br>
12 * Type: outputfilter<br>
13 * Name: trimwhitespace<br>
14 * Date: Jan 25, 2003<br>
15 * Purpose: trim leading white space and blank lines from
16 * template source after it gets interpreted, cleaning
17 * up code and saving bandwidth. Does not affect
18 * <<PRE>></PRE> and <SCRIPT></SCRIPT> blocks.<br>
19 * Install: Drop into the plugin directory, call
20 * <code>$template_object->load_filter('output','trimwhitespace');</code>
22 * @author Monte Ohrt <monte at ohrt dot com>
23 * @author Contributions from Lars Noschinski <lars@usenet.noschinski.de>
29 function template_outputfilter_trimwhitespace($tpl_source, &$template_object)
31 // Pull out the script blocks
32 preg_match_all("!<script[^>]+>.*?</script>!is", $tpl_source, $match);
33 $_script_blocks = $match[0];
34 $tpl_source = preg_replace("!<script[^>]+>.*?</script>!is",
35 '@@@TEMPLATELITE:TRIM:SCRIPT@@@', $tpl_source);
37 // Pull out the pre blocks
38 preg_match_all("!<pre>.*?</pre>!is", $tpl_source, $match);
39 $_pre_blocks = $match[0];
40 $tpl_source = preg_replace("!<pre>.*?</pre>!is",
41 '@@@TEMPLATELITE:TRIM:PRE@@@', $tpl_source);
43 // Pull out the textarea blocks
44 preg_match_all("!<textarea[^>]+>.*?</textarea>!is", $tpl_source, $match);
45 $_textarea_blocks = $match[0];
46 $tpl_source = preg_replace("!<textarea[^>]+>.*?</textarea>!is",
47 '@@@TEMPLATELITE:TRIM:TEXTAREA@@@', $tpl_source);
49 // remove all leading spaces, tabs and carriage returns NOT
50 // preceeded by a php close tag.
51 $tpl_source = trim(preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $tpl_source));
53 // replace script blocks
54 template_outputfilter_trimwhitespace_replace("@@@TEMPLATELITE:TRIM:SCRIPT@@@",$_script_blocks, $tpl_source);
57 template_outputfilter_trimwhitespace_replace("@@@TEMPLATELITE:TRIM:PRE@@@",$_pre_blocks, $tpl_source);
59 // replace textarea blocks
60 template_outputfilter_trimwhitespace_replace("@@@TEMPLATELITE:TRIM:TEXTAREA@@@",$_textarea_blocks, $tpl_source);
65 function template_outputfilter_trimwhitespace_replace($search_str, $replace, &$subject) {
66 $_len = strlen($search_str);
68 for ($_i=0, $_count=count($replace); $_i<$_count; $_i++
)
70 if (($_pos=strpos($subject, $search_str, $_pos))!==false)
72 $subject = substr_replace($subject, $replace[$_i], $_pos, $_len);