英文:
Combine html templates into one in GAE GO base template so that the structure would only have a common html/css structure
问题
在这个例子中,我有一个main.html模板
<!DOCTYPE html>
<html>
<head>
<title>Backend</title>
<style>
html, body {height:100%}
</style>
</head>
<body>
<table border="1" width="100%" height="100%">
<tr>
<td colspan="2" class="td-header">
<h1>Google GO</h1>
</td>
</tr>
<tr>
<td class="td-right-content">
{{<parsed template from children>}}
</td>
</tr>
<tr>
<td colspan="2" class="td-header">
<h1>Footer</h1>
</td>
</tr>
</table>
</body>
</html>
子部分将填充
{{
与
<table>
<tr>
<th>
Name
</th>
<th>
Description
</th>
<th>
</th>
</tr>
{{range .}}
<tr>
<td>
{{.Name}}
</td>
<td>
{{.Description}}
</td>
<td>
<a href="/admin/forms/edit/?key={{.Key.Encode}}">Edit</a>
</td>
</tr>
{{end}}
</table>
在子部分的代码中解析后。
我这样做是为了消除冗余的HTML和CSS,并且方便管理设计。
谢谢大家!
英文:
In this example I have a main.html template
<!DOCTYPE html>
<html>
<head>
<title>Backend</title>
<style>
html, body {height:100%}
</style>
</head>
<body>
<table border="1" width="100%" height="100%">
<tr>
<td colspan="2" class="td-header">
<h1>Google GO</h1>
</td>
</tr>
<tr>
<td class="td-right-content">
{{<parsed template from children>}}
</td>
</tr>
<tr>
<td colspan="2" class="td-header">
<h1>Footer</h1>
</td>
</tr>
</table>
</body>
</html>
The child part would fill the
{{<parsed template from children>}}
With
<table>
<tr>
<th>
Name
</th>
<th>
Description
</th>
<th>
</th>
</tr>
{{range .}}
<tr>
<td>
{{.Name}}
</td>
<td>
{{.Description}}
</td>
<td>
<a href="/admin/forms/edit/?key={{.Key.Encode}}">Edit</a>
</td>
</tr>
{{end}}
</table>
After it has been parsed in the code of child part.
I am doing this to eliminate redundant html and css and to manage the design easily.
Thanks all!
答案1
得分: 2
一个Template
对象包含一个顶级模板(这里是父模板),它可以引用同一对象中的其他模板。模板有一个用于引用的名称。
这可能有点棘手,因为当你使用ParseFiles
函数创建一个新对象时,每个模板都使用文件的基本名称命名(似乎无法更改该名称)。如果对于给定的主模板有多个可能的子文件,这可能不方便,因为通常不希望给它们相同的名称。
解决方案是手动将文件读取为字符串,然后将其添加到一个明确命名的模板中(我认为有点繁琐,但你可以接受)。
main_temp,_ := template.ParseFiles("main.html")
cont_s,_ := ioutil.ReadFile("content1.html")
// 添加一个新的关联模板到主模板
cont_temp,_ := main_temp.New("content").Parse(string(cont_s))
g := Content{"Hi"}
main_temp.Execute(os.Stdout, &g)
(我省略了示例中的所有错误处理)
然后你可以在父页面中使用{{template}}
指令:
{{template "content" .}}
(或者使用任何管道代替.
,它引用给定给main_temp
的整个对象)
更多详细信息请参阅text/template文档
英文:
A Template
object contains a top-level template (here: the parent template) which may reference other templates associated in the same object. Templates have a name used for referencement.
It can be tricky, because when you use the ParseFiles
function to create a new object, each template is named using the base name of the file (and it seems to be impossible to change that name). If you have multiple possible children files for a given main, it can be impractical because you usually don't want to give them the same name.
The solution it to manually read the file to a string and then add it to an explicitly named template (a little bit cumbersome IMO, but you can live with it).
main_temp,_ := template.ParseFiles("main.html")
cont_s,_ := ioutil.ReadFile("content1.html")
// add a new associated template to main
cont_temp,_ := main_temp.New("content").Parse(string(cont_s))
g := Content{"Hi"}
main_temp.Execute(os.Stdout, &g)
(I skipped all the error handling for the example)
Then you can use the {{template}}
directive in your parent page:
{{template "content" .}}
(or any pipeline instead of .
which is referring to the whole object given to main_temp
)
See text/template doc for more details
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论