Smarty 缩进和代码格式
我可以用 Smarty 制作漂亮的 HTML 吗?
我的意思是,如果我采用这种模式(它不是一个工作代码,只是一个示例):
<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>
它可能会产生类似这样的东西
<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>
看看所有这些丑陋的空格和换行符
所以问题是:是否有任何做法可以避免使用 Smarty 来避免丑陋的代码?
也许我需要使用这样的东西?
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您想要格式化的方式,您可以使用 {strip} 函数: http://www.smarty.net/docs/en/language.function.strip.tpl {strip} 从输出中删除空格。
如果您希望输出在一行上,您可以执行以下操作:
您还可以使用 {strip} 删除部分输出中的空格:
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:
You can also use {strip} to remove whitespace in parts of the output:
例如:
For example: