根据请求 URL 在主模板中执行模板片段
Execute template fragment in main template based on requested URL
不确定如何正确命名它。
有什么方法可以编写一个主模板和许多片段,并根据 URL 用户请求注入所需的片段。
假设我有 /customers/profile 和 /customers/projects。我想写一个主要的 customer.html 模板文件和一个 customer-includes.html 文件和 2 {{ 定义 "profile" }} 和 {{ 定义 "projects" }} 片段。
然后我想要 2 个处理程序来处理 /customers/profile 和 /customers/projects 并执行 customer.html模板。
但是,当用户转到 URL /customers/profile 时,我想在主模板 {{ 模板 "profile" 中注入。 }} 如果他去 /customers/projects 我想注入 {{ template "projects" 。 }}。
做这个的最好方式是什么?
我假设我需要在那里使用某种 {{ if / else }} 。如下例。但是mby有更好的方法。
{{ if ( eq .Section "customer-profile") }} // Could be replaced with Page ID
{{ template "profile" . }}
{{ else }}
{{ template "projects" . }}
{{ end}}
您可以为此使用模板块。
templates/customers-base.html
:
<html>
<head>
<title>{{.title}}</title>
<link rel="stylesheet" type="text/css" href="static/styles.css">
<!-- You can include common scripts and stylesheets in the base template -->
</head>
<body>
{{block "BODY" .}}
{{end}}
</body>
</html>
templates/customers-projects.html
:
{{define "BODY"}}
<h1>Your Projects</h1>
<p>Normal template goes here</p>
<p>{{.myvar}}<p>
{{end}}
您可以为 templates/customers-profile.html
复制此格式。
您的项目代码:
data := map[string]interface{}{
"title": "Base template example",
"myvar": "Variable example",
}
layoutCustomersBase := template.Must(template.ParseFiles("templates/customers-base.html"))
layoutCustomersProjects := template.Must(layoutCustomersBase.ParseFiles("templates/customers-projects.html"))
// Or layoutCustomersProfile, if you are parsing in the '/customers/profile' handler.
err := layoutError.Execute(w, data)
注意在执行customers-projects
模板时可以定义"title"变量;它将在基本模板中使用。
不确定如何正确命名它。
有什么方法可以编写一个主模板和许多片段,并根据 URL 用户请求注入所需的片段。 假设我有 /customers/profile 和 /customers/projects。我想写一个主要的 customer.html 模板文件和一个 customer-includes.html 文件和 2 {{ 定义 "profile" }} 和 {{ 定义 "projects" }} 片段。 然后我想要 2 个处理程序来处理 /customers/profile 和 /customers/projects 并执行 customer.html模板。 但是,当用户转到 URL /customers/profile 时,我想在主模板 {{ 模板 "profile" 中注入。 }} 如果他去 /customers/projects 我想注入 {{ template "projects" 。 }}。 做这个的最好方式是什么? 我假设我需要在那里使用某种 {{ if / else }} 。如下例。但是mby有更好的方法。
{{ if ( eq .Section "customer-profile") }} // Could be replaced with Page ID
{{ template "profile" . }}
{{ else }}
{{ template "projects" . }}
{{ end}}
您可以为此使用模板块。
templates/customers-base.html
:
<html>
<head>
<title>{{.title}}</title>
<link rel="stylesheet" type="text/css" href="static/styles.css">
<!-- You can include common scripts and stylesheets in the base template -->
</head>
<body>
{{block "BODY" .}}
{{end}}
</body>
</html>
templates/customers-projects.html
:
{{define "BODY"}}
<h1>Your Projects</h1>
<p>Normal template goes here</p>
<p>{{.myvar}}<p>
{{end}}
您可以为 templates/customers-profile.html
复制此格式。
您的项目代码:
data := map[string]interface{}{
"title": "Base template example",
"myvar": "Variable example",
}
layoutCustomersBase := template.Must(template.ParseFiles("templates/customers-base.html"))
layoutCustomersProjects := template.Must(layoutCustomersBase.ParseFiles("templates/customers-projects.html"))
// Or layoutCustomersProfile, if you are parsing in the '/customers/profile' handler.
err := layoutError.Execute(w, data)
注意在执行customers-projects
模板时可以定义"title"变量;它将在基本模板中使用。