英文:
How can I control whitespace after an action in html/template?
问题
我遇到了一个问题,无法控制空白并以可读的方式格式化html/template
模板。我的模板看起来像这样:
layout.tmpl
{{define "layout"}}
<!DOCTYPE html>
<html>
<head>
<title>{{.title}}</title>
</head>
<body>
{{ template "body" . }}
</body>
</html>
{{end}}
body.tmpl
{{define "body"}}
{{ range .items }}
{{.count}} items are made of {{.material}}
{{end}}
{{end}}
代码
package main
import (
"os"
"text/template"
)
type View struct {
layout string
body string
}
type Smap map[string]string
func (self View) Render(data map[string]interface{}) {
layout := self.layout + ".tmpl"
body := self.body + ".tmpl"
tmpl := template.Must(template.New("layout").ParseFiles(layout, body))
tmpl.ExecuteTemplate(os.Stdout, "layout", data)
}
func main() {
view := View{ "layout", "body" }
view.Render(map[string]interface{}{
"title": "stock",
"items": []Smap{
Smap{
"count": "2",
"material": "angora",
},
Smap{
"count": "3",
"material": "wool",
},
},
})
}
但是这样会产生以下结果(注意:doctype上面有一行空白):
<!DOCTYPE html>
<html>
<head>
<title>stock</title>
</head>
<body>
2 items are made of angora
3 items are made of wool
</body>
</html>
我想要的是:
<!DOCTYPE html>
<html>
<head>
<title>stock</title>
</head>
<body>
2 items are made of angora
3 items are made of wool
</body>
</html>
在其他模板语言中,我可以这样写:
[[- value -]]
并且操作前后的空白会被删除,但是在html/template
中我没有看到类似的东西。这是否意味着我必须像下面这样使我的模板不可读?
layout.tmpl
{{define "layout"}}<!DOCTYPE html>
<html>
<head>
<title>.title</title>
</head>
<body>
{{ template "body" . }}</body>
</html>
{{end}}
body.tmpl
{{define "body"}}{{ range .items }}{{.count}} items are made of {{.material}}
{{end}}{{end}}
英文:
I am having a problem controlling whitespace and still formatting html/template
templates in a readable fashion. My templates look somthing like this:
layout.tmpl
{{define "layout"}}
<!DOCTYPE html>
<html>
<head>
<title>{{.title}}</title>
</head>
<body>
{{ template "body" . }}
</body>
</html>
{{end}}
body.tmpl
{{define "body"}}
{{ range .items }}
{{.count}} items are made of {{.material}}
{{end}}
{{end}}
code
package main
import (
"os"
"text/template"
)
type View struct {
layout string
body string
}
type Smap map[string]string
func (self View) Render(data map[string]interface{}) {
layout := self.layout + ".tmpl"
body := self.body + ".tmpl"
tmpl := template.Must(template.New("layout").ParseFiles(layout, body))
tmpl.ExecuteTemplate(os.Stdout, "layout", data)
}
func main() {
view := View{ "layout", "body" }
view.Render(map[string]interface{}{
"title": "stock",
"items": []Smap{
Smap{
"count": "2",
"material": "angora",
},
Smap{
"count": "3",
"material": "wool",
},
},
})
}
But that produces (note: there is a line above the doctype):
<!DOCTYPE html>
<html>
<head>
<title>stock</title>
</head>
<body>
2 items are made of angora
3 items are made of wool
</body>
</html>
What I want is:
<!DOCTYPE html>
<html>
<head>
<title>stock</title>
</head>
<body>
2 items are made of angora
3 items are made of wool
</body>
</html>
In other template languages I can say things like
[[- value -]]
and the whitespace before and after the action are stripped, but I don't see anything like that in html/template
. Does this really mean I have to make my templates unreadable like the following?
layout.tmpl
{{define "layout"}}<!DOCTYPE html>
<html>
<head>
<title>.title</title>
</head>
<body>
{{ template "body" . }} </body>
</html>
{{end}}
body.tmpl
{{define "body"}}{{ range .items }}{{.count}} items are made of {{.material}}
{{end}}{{end}}
答案1
得分: 20
你可以使用空格控制器
{{range .foos -}} // 吃掉尾部空格
<tr><td>做一些事情</td></tr>
{{- end}} // 吃掉前导空格(来自上一行的\n)
英文:
You can use white space controller
{{range .foos -}} // eats trailing whitespace
<tr><td>do something</td></tr>
{{- end}} // eats leading whitespace (\n from previous line)
答案2
得分: 2
在这种情况下,Whitespace对用户浏览器中的呈现输出没有影响,因此控制它在美观方面可能没有太大意义。
换句话说,可以有格式良好的模板(我更喜欢这种方式),或者部分格式良好的HTML(没有嵌套缩进)。选择其中一种方式或者使用任何现有的格式化工具对HTML进行后处理。
英文:
Whitespace in this case makes no difference in the rendered output at the user's browser, so controlling it makes little sense above perhaps aesthetics.
Put differently, one can have nicely formatted templates (which I would prefer) or partially nicely formatted HTML (no nested indents). Pick one or post process the HTML using any of the existing formatters.
答案3
得分: 1
是的,空格和换行符会被逐字翻译。如果你有一行只有一个{{ define }}或其他不产生输出的内容,解析后的文件中将会有一行空行。
理想情况下,因为你正在使用模板,你不需要查看或编辑解析后的输出。作为参考,可以使用JSP/JSF并查看它给出的丑陋输出。查看大多数在线页面的源代码,它们也很丑陋。
祝你好运!
英文:
Yes, whitespace and lines are translated literally. If you have a line with just a {{ define }} or anything else that does not produce output, you will have an empty line in the parsed file.
Ideally, because you are using template, you should not need to view or edit the parsed output. For reference, use JSP/JSF and see the ugly output it gives you. View the source of most pages online, they're also ugly.
Good luck!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论