2

We heavily use Smarty for all our front end templating. I have observed the following situation:

When my Smarty template is similar to as follows:

<li>
    {if $a == 'A'}
        {$var1}
    {else}
        {$var2}
    {/if}
    <br><span>SUBTEXT</span>
</li>

The final HTML which is delivered to the browser is:

<li>
                            65
                        <br><span>SUBTEXT</span>
        </li>

I would expect it to be more clean and something like:

<li>
    65<br><span>SUBTEXT</span>
</li>

or better:

<li>65<br><span>SUBTEXT</span></li>

Anyway I can do this with some configuration settings in Smarty 3? Any setting to format and clean the final HTML created?

Thanks

Sparsh Gupta
  • 2,163
  • 5
  • 19
  • 21
  • Not sure how relevant this still is by now, but it may help: [Stripping whitespace out of smarty templates from PHP](http://stackoverflow.com/questions/3558464/stripping-whitespace-out-of-smarty-templates-from-php) – Wesley Murch Feb 09 '12 at 08:07

3 Answers3

7

You can use {strip} to remove all white space and carriage returns in part of a template:

http://www.smarty.net/docsv2/en/language.function.strip.tpl

{strip}
<li>
    {if $a == 'A'}
        {$var1}
    {else}
        {$var2}
    {/if}
    <br><span>SUBTEXT</span>
</li>
{/strip}

Output should be:

<li>65<br><span>SUBTEXT</span></li>

This may be inconvenient, but be aware that white space and newlines have a significant impact/importance on the HTML output, and stripping them globally can have unintended side effects.

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
  • I have a number of different templates which include each other etc. and I tried giving strip to the master template but it seems I need to do it with every template. Not sure if its the best way – Sparsh Gupta Feb 09 '12 at 09:50
  • It seems like wasted effort to me, unless it's actually affecting your output. Are you just trying to reduce file size or are you doing it so you can look at your source code after it's sent to the browser? The browser and end user both don't care about extra spaces or newlines in HTML source, and there are debugging tools for source code inspection. The only time I can think of that you need this is for some instances where `display:inline-block` is getting affected by white space and you want to remove it. – Wesley Murch Feb 09 '12 at 09:58
6

You can load the output filter trimwhitespace. It removes HTML comments (except ConditionalComments) and reduces multiple whitespace to a single space everywhere but <script>, <pre>, <textarea>.

You can easily make the filter remove space between <two> <tags> by altering line 62. change

'#(:SMARTY@!@|>)\s+(?=@!@SMARTY:|<)#s' => '\1 \2',

to

'#(:SMARTY@!@|>)\s+(?=@!@SMARTY:|<)#s' => '\1\2',

and you're done.

Output filters run AFTER the template is rendered and BEFORE it's sent to the browser. {strip} runs before the template is processed - it's a compile-time thing. So the following

{$some_var = "Hello\nworld"}
{strip}
  -
  {$}
  -
{/strip}

will output

-hello
world-

while the outputfilter would return

- hello world -
Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
rodneyrehm
  • 13,442
  • 1
  • 40
  • 56
  • Removing space between ` ` can be a bad idea, (e.g.`Hello World` becomes `HelloWorld`) but I think this default filter is what OP wants. Any reason `` isn't in that list? EDIT: LOL Smarty developer gets downvoted on a Smarty question? What's going on here? – Wesley Murch Feb 09 '12 at 08:28
  • because removing all spaces is generally a bad idea, I wrote the plugin the way I did. `` only works right if your CSS knows that there are "missing spaces" and fixes that for you - Downvoted? huh? – rodneyrehm Feb 09 '12 at 08:44
  • I meant someone -1'd your post for some unknown reason, I just found it ironic because you're a Smarty dev. Anyways, you misunderstood me, I was wondering why the default whitespace formatter doesn't ignore ``. Not sure what you mean by the CSS bit though. Missing whitespace in a body of text `Likethis` cannot really be fixed by CSS. – Wesley Murch Feb 09 '12 at 08:47
  • `` is an inline-element. Line-breaks and multiple spaces have no meaning here. With Smarty 3.2 we're setting the base to make all plugins configurable. I'm planning on adding a configuration facility so you could change the to-be-ignored-tags yourself, as they may differ from project to project. – rodneyrehm Feb 09 '12 at 09:04
  • Ah that's correct, `` does not necessarily mean preformatted. – Wesley Murch Feb 09 '12 at 09:07
1

If you want to remove the whitespace from all files simple call:

$oSmarty->loadFilter("output", "trimwhitespace");
$oSmarty->display($display);

Further info have a look at PHP class: smarty_outputfilter_trimwhitespace and docs: http://www.smarty.net/docs/en/advanced.features.outputfilters.tpl

John Magnolia
  • 16,769
  • 36
  • 159
  • 270