如何在html/template中控制动作后的空白?

huangapple go评论91阅读模式
英文:

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 &quot;layout&quot;}}
&lt;!DOCTYPE html&gt;
&lt;html&gt;
        &lt;head&gt;
                &lt;title&gt;{{.title}}&lt;/title&gt;
        &lt;/head&gt;
        &lt;body&gt;
                {{ template &quot;body&quot; . }}
        &lt;/body&gt;
&lt;/html&gt;
{{end}}

body.tmpl

{{define &quot;body&quot;}}
{{ range .items }}
{{.count}} items are made of {{.material}}
{{end}}
{{end}}

code

package main

import (
	&quot;os&quot;
	&quot;text/template&quot;
)

type View struct {
	layout string
	body   string
}

type Smap map[string]string

func (self View) Render(data map[string]interface{}) {
	layout := self.layout + &quot;.tmpl&quot;
	body   := self.body + &quot;.tmpl&quot;
	tmpl   := template.Must(template.New(&quot;layout&quot;).ParseFiles(layout, body))
	tmpl.ExecuteTemplate(os.Stdout, &quot;layout&quot;, data)
}

func main() {
	view := View{ &quot;layout&quot;, &quot;body&quot; }
	view.Render(map[string]interface{}{
		&quot;title&quot;: &quot;stock&quot;,
		&quot;items&quot;: []Smap{
			Smap{
				&quot;count&quot;:    &quot;2&quot;,
				&quot;material&quot;: &quot;angora&quot;,
			},
			Smap{
				&quot;count&quot;:    &quot;3&quot;,
				&quot;material&quot;: &quot;wool&quot;,
			},
		},
	})
}

But that produces (note: there is a line above the doctype):

&lt;!DOCTYPE html&gt;
&lt;html&gt;
	&lt;head&gt;
		&lt;title&gt;stock&lt;/title&gt;
	&lt;/head&gt;
	&lt;body&gt;


2 items are made of angora

3 items are made of wool


	&lt;/body&gt;
&lt;/html&gt;

What I want is:

&lt;!DOCTYPE html&gt;
&lt;html&gt;
	&lt;head&gt;
		&lt;title&gt;stock&lt;/title&gt;
	&lt;/head&gt;
	&lt;body&gt;


2 items are made of angora

3 items are made of wool


	&lt;/body&gt;
&lt;/html&gt;

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 &quot;layout&quot;}}&lt;!DOCTYPE html&gt;
&lt;html&gt;
	&lt;head&gt;
		&lt;title&gt;.title&lt;/title&gt;
	&lt;/head&gt;
	&lt;body&gt;
{{ template &quot;body&quot; . }}	&lt;/body&gt;
&lt;/html&gt;
{{end}}

body.tmpl

{{define &quot;body&quot;}}{{ range .items }}{{.count}} items are made of {{.material}}
{{end}}{{end}}

答案1

得分: 20

你可以使用空格控制器

{{range .foos -}} // 吃掉尾部空格
    &lt;tr&gt;&lt;td&gt;做一些事情&lt;/td&gt;&lt;/tr&gt;
{{- end}} // 吃掉前导空格(来自上一行的\n)
英文:

You can use white space controller

{{range .foos -}} // eats trailing whitespace
    &lt;tr&gt;&lt;td&gt;do something&lt;/td&gt;&lt;/tr&gt;
{{- 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!

huangapple
  • 本文由 发表于 2013年7月6日 20:18:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/17502891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定