7

can I make a good looking HTML with Smarty?

I mean if I take this pattern (it's not a working code, just an example):

<div class="comments-div">
    {{assign var="i" value="0"}}
    {{assign var="tab" value="0"}}
    {{foreach from=$contact.comments item=comment}}
    <div class="comment-text"><p>{{$comment.text}}</p></div>
        {{if $i == 3}}
            {{assign var="i" value="0"}}
        {{else}}
            {{assign var="i" value=`$i+1`}}
        {{/if}}
    {{/foreach}}
</div>

It may produce something like this

                                  <div class="tab" id="tab0" style="display: block;">
            <div id="container73">
                <div class="comment-text"><p>c1</p></div>
                <div class="addby">
                    <p>
                    Added by: ASDF at 2011-04-22 15:58:41
                                            &nbsp;<span> | </span>&nbsp;<a class="delete" _id="73">Delete comment</a>

                                            </p>
                </div>
            </div>
                                                                        <div id="container74">
                <div class="comment-text"><p>c2</p></div>
                <div class="addby">
                    <p>
                    Added by: DFGS at 2011-04-22 15:58:44
                                            &nbsp;<span> | </span>&nbsp;<a class="delete" _id="74">Delete comment</a>

                                            </p>
                </div>
            </div>

Look at all this ugly spaces and newlines

So question is: is there any practices to avoid ugly code with Smarty?

Maybe I need to use something like this?

<div class="comments-div">
{{    assign var="i" value="0"}}
{{    assign var="tab" value="0"}}
{{    foreach from=$contact.comments item=comment}}
    <div class="comment-text"><p>{{$comment.text}}</p></div>
{{        if $i == 3}}
{{            assign var="i" value="0"}}
{{        else}}
{{            assign var="i" value=`$i+1`}}
{{        /if}}
{{    /foreach}}
</div>
llamerr
  • 2,997
  • 4
  • 29
  • 40
  • 1
    xml itself is ugly, consider using something more clean such as [phaml](http://phaml.sourceforge.net/) – denysonique Apr 22 '11 at 15:45
  • Also you should separate your application logic from its views(templates), try to to follow the MVC pattern, this will make everything more readable. – denysonique Apr 22 '11 at 15:52
  • so in template engine(smarty) for template language(php) i must use one more logic/view separation? – llamerr Apr 22 '11 at 16:34
  • 1
    `{{if $i == 3}} {{assign var="i" value="0"}} {{else}} {{assign var="i" value=$i+1}}` This doesn't really look nice. In Rails for example you would replace this with a helper method, define this code elsewhere and use a helper method e.g. {{show_karma}} in place of that code in the template. – denysonique Apr 23 '11 at 00:18
  • Looks strange for me, with my experience, but if it used in RoR I think it must be right sometimes. Maybe I will look for it later, thanks for answer – llamerr Apr 26 '11 at 10:44

2 Answers2

2

Depending on how you want to format it, you can use the {strip} function: http://www.smarty.net/docs/en/language.function.strip.tpl {strip} removes whitespace from the output.

If you want the output on one line, you could do something like this:

{{strip}}<div class="comments-div">
    {{assign var="i" value="0"}}
    {{assign var="tab" value="0"}}
    {{foreach from=$contact.comments item=comment}}
    <div class="comment-text"><p>{{$comment.text}}</p></div>
        {{if $i == 3}}
            {{assign var="i" value="0"}}
        {{else}}
        {{assign var="i" value=`$i+1`}}
        {{/if}}
    {{/foreach}}
</div>{{/strip}}

You can also use {strip} to remove whitespace in parts of the output:

<div class="comments-div">{{strip}}
    {{assign var="i" value="0"}}
    {{assign var="tab" value="0"}}
    {{/strip}}{{foreach from=$contact.comments item=comment}}
    <div class="comment-text"><p>{{$comment.text}}</p></div>{{strip}}
        {{if $i == 3}}
            {{assign var="i" value="0"}}
        {{else}}
        {{assign var="i" value=`$i+1`}}
        {{/if}}
    {{/strip}}{{/foreach}}
</div>
Michael Dean
  • 1,506
  • 9
  • 11
0

For example:

$html = $smarty->fetch('index.tpl');
$html = preg_replace("#^\s+#m", '', $html);
echo $html;
Sergey L
  • 547
  • 2
  • 5
  • 19