Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Templates 如何在html/模板中控制操作后的空白?_Templates_Go_Whitespace_Go Html Template - Fatal编程技术网

Templates 如何在html/模板中控制操作后的空白?

Templates 如何在html/模板中控制操作后的空白?,templates,go,whitespace,go-html-template,Templates,Go,Whitespace,Go Html Template,我在控制空白和以可读方式格式化html/template模板时遇到问题。我的模板看起来像这样: layout.tmpl {{define "layout"}} <!DOCTYPE html> <html> <head> <title>{{.title}}</title> </head> <body> {

我在控制空白和以可读方式格式化
html/template
模板时遇到问题。我的模板看起来像这样:

layout.tmpl

{{define "layout"}}
<!DOCTYPE html>
<html>
        <head>
                <title>{{.title}}</title>
        </head>
        <body>
                {{ template "body" . }}
        </body>
</html>
{{end}}
{{define "layout"}}<!DOCTYPE html>
<html>
    <head>
        <title>.title</title>
    </head>
    <body>
{{ template "body" . }} </body>
</html>
{{end}}
代码

但这会产生(注意:doctype上方有一行):

操作前后的空白都被删除了,但我在
html/template
中没有看到类似的内容。这是否真的意味着我必须使我的模板无法阅读,如下所示

layout.tmpl

{{define "layout"}}
<!DOCTYPE html>
<html>
        <head>
                <title>{{.title}}</title>
        </head>
        <body>
                {{ template "body" . }}
        </body>
</html>
{{end}}
{{define "layout"}}<!DOCTYPE html>
<html>
    <head>
        <title>.title</title>
    </head>
    <body>
{{ template "body" . }} </body>
</html>
{{end}}

是的,空格和行是按字面翻译的。如果有一行只有一个{{define}}或其他任何不产生输出的内容,那么解析文件中将有一个空行

理想情况下,因为您使用的是模板,所以不需要查看或编辑解析后的输出。作为参考,请使用JSP/JSF并查看它提供给您的丑陋输出。在网上查看大多数网页的来源,它们也很难看

祝你好运

在这种情况下,用户浏览器上的渲染输出没有任何区别,因此控制它可能没有什么意义


换句话说,可以有格式良好的模板(我更喜欢)或部分格式良好的HTML(没有嵌套缩进)。选择一个或使用任何现有格式化程序对HTML进行后期处理。

您可以使用空白控制器

{{range .foos -}} // eats trailing whitespace
    <tr><td>do something</td></tr>
{{- end}} // eats leading whitespace (\n from previous line)
{{range.foos-}}//
做点什么
{{-end}}//吃前导空格(\n来自前一行)

是的,谢谢@webwurst 1.6之前的版本,您可以检查此项仅适用于
text/template
,而不适用于
html/template
[[- value -]]
{{define "layout"}}<!DOCTYPE html>
<html>
    <head>
        <title>.title</title>
    </head>
    <body>
{{ template "body" . }} </body>
</html>
{{end}}
{{define "body"}}{{ range .items }}{{.count}} items are made of {{.material}}
{{end}}{{end}}
{{range .foos -}} // eats trailing whitespace
    <tr><td>do something</td></tr>
{{- end}} // eats leading whitespace (\n from previous line)