在使用Play Framework2时,我注意到呈现的Scala HTML模板不喜欢缩进的@if或@for。
举个例子,类似这样的东西:
<ul>
@for(test <- tests) {
<li>@test.name</li>
}
</ul>会有额外的不需要的空格。为了修复它,我需要这样做:
<ul>
@for(test <- tests) {
<li>@test.name</li>
}
</ul>这将使额外的@defining或其他语句变得混乱。
那么,有没有一种方法可以美化/美化Scala模板渲染,以消除多余的空格?
更新:
阅读this thread时,我注意到由于模板顶部的参数,还添加了额外的空格和换行符。所以这就是:
@(myParam: String)
<!DOCTYPE html>
<html>
<head></head>
<body></body>
</html>将在生成的HTML顶部添加3个额外的换行符。这绝对很烦人。
这个帖子似乎在说,目前没有解决这个问题的办法。
发布于 2013-01-04 19:24:38
当然,总是有一些选项:),裁剪主体并再次设置头部(因为在对字符串进行操作后,它将作为text/plain返回):
// instead of
return ok(index.render("some"));
// use
return ok(index.render("some").body().trim()).as("text/html; charset=utf-8");对于‘美容’循环,或者如果你需要写更紧凑的代码
// instead of
@for(test <- tests) {
<li>@test.name</li>
}
// use
@for(test <- tests) {<li>@test.name</li>}最后,你可以使用一些压缩器(即,com.googlecode.htmlcompressor)到...缩小整个页面(在此示例中仅适用于生产模式)
String output = index.render("some").body().trim();
if (Play.isProd()) output = compressor.compress(output);
return ok(output).as("text/html; charset=utf-8");https://stackoverflow.com/questions/14154671
复制相似问题