Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smarty indentations and code format

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>
like image 857
llamerr Avatar asked Apr 22 '11 15:04

llamerr


1 Answers

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>
like image 155
Michael Dean Avatar answered Oct 14 '22 15:10

Michael Dean