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>
5
  • 1
    xml itself is ugly, consider using something more clean such as phaml Apr 22, 2011 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. Apr 22, 2011 at 15:52
  • so in template engine(smarty) for template language(php) i must use one more logic/view separation?
    – llamerr
    Apr 22, 2011 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. Apr 23, 2011 at 0: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, 2011 at 10:44

2 Answers 2

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>
0

For example:

$html = $smarty->fetch('index.tpl');
$html = preg_replace("#^\s+#m", '', $html);
echo $html;

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.