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
<span> | </span> <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
<span> | </span> <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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With