速度指令也添加空格吗?
我刚刚了解到,使用 apache Velocity 指令也会添加到空格中。
例如:
#foreach ($record in $rows)
#foreach($value in $record)
$value
#end
#end
对于这样的事情,我最终会为 #foreach
语句、#end
语句等获得额外的行。
这不是我想要的,所以我发现我可以像这样阻止行尾的评论:
#foreach ($record in $rows)#*
*##foreach($value in $record)#*
*#$value #*
*##end
#end
但这读起来非常难看。有什么方法可以告诉速度引擎不要格式化我的指令吗?
也许我做错了什么?
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你被困住了(参见 Velocity Whitespace Gobbling 文章),尽管行注释将是更整洁一些:
或者你可以将所有内容压缩到一行中:
I think you're stuck with it (see Velocity Whitespace Gobbling article) although line comments would be a little tidier:
Or you could just squeeze everything onto one line:
这实际上是几乎所有模板语言的共同点,其推理直接来自简化处理。考虑下面的例子(这实际上是 Grails 使用的 GSP,但想法是相同的):
处理的方式是首先是一个标签(或者在 Velocity 的情况下,指令) >) 被识别。由于标签/指令本身包含用于处理标签主体的指令,因此标签/指令标记被移除,并且紧接在开始标记之后和紧接在结束标记之前的所有内容被用作处理目标。这包括所有空白,因为提前清理输出会困难得多。
这当然并不意味着您不能这样做,正如 Edd 指出的那样,或者这首先是最明智的设计选择,但有时更简单地做事比生成漂亮的标记更重要 - 毕竟,大多数(如果不是全部)标记处理器并不真正关心您是否有
some\ncontent
或
一些\n\n\n\t\t内容
。This is actually common to almost all templating languages and the reasoning comes directly from simplified processing. Consider the following example (this is actually GSP used by Grails but the idea is the same):
The way this is processed is that first a tag (or in Velocity's case, directive) is identified. Because the tag/directive itself contains instructions for processing the tag's body, the tag/directive marks are removed and all the content immediately after the starting mark and immediately before the ending mark is used as the target for the processing. This includes all the whitespace, because cleaning the output beforehand would be a lot more difficult.
This of course doesn't mean that you can't do it, as Edd points out or that this would be the most sensible design choice in the first place, but sometimes doing things more simply is more important than generating beautiful markup - after all, most if not all markup processors don't really care if you have
<p>some\ncontent</p>
or<p>some\n\n\n\t\tcontent</p>
.