速度指令也添加空格吗?

发布于 2024-12-20 20:19:45 字数 459 浏览 5 评论 0 原文

我刚刚了解到,使用 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

但这读起来非常难看。有什么方法可以告诉速度引擎不要格式化我的指令吗?

也许我做错了什么?

谢谢。

I've just learned that with apache velocity the directives add to the whitespace as well.

So for example:

#foreach ($record in $rows)
#foreach($value in $record)
$value  
#end

#end

With something like this I end up getting extra lines for the #foreach statements, the #end statements, etc.

This isn't what I want, so I found I could block comment at the end of the lines like so:

#foreach ($record in $rows)#*
*##foreach($value in $record)#*
*#$value    #*
*##end

#end

But this is pretty ugly to read. Is there any way to tell the velocity engine to not format my directives?

Maybe I'm doing something else wrong?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请叫√我孤独 2024-12-27 20:19:45

我认为你被困住了(参见 Velocity Whitespace Gobbling 文章),尽管行注释将是更整洁一些:

#foreach ($record in $rows)##
#foreach($value in $record)##
$value    ##
#end

#end

或者你可以将所有内容压缩到一行中:

#foreach($record in $rows)#foreach($value in $record)${value}#{end}#{end}

I think you're stuck with it (see Velocity Whitespace Gobbling article) although line comments would be a little tidier:

#foreach ($record in $rows)##
#foreach($value in $record)##
$value    ##
#end

#end

Or you could just squeeze everything onto one line:

#foreach($record in $rows)#foreach($value in $record)${value}#{end}#{end}
天煞孤星 2024-12-27 20:19:45

这实际上是几乎所有模板语言的共同点,其推理直接来自简化处理。考虑下面的例子(这实际上是 Grails 使用的 GSP,但想法是相同的):

<g:each var="x" in="exes">
    ${x.y}
</g:each>

处理的方式是首先是一个标签(或者在 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):

<g:each var="x" in="exes">
    ${x.y}
</g:each>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文