В golang, я работаю с тремя файлами: index.html, nav.html и main.goGolang Доступ Переменные шаблона из включённых Шаблоны
nav.html содержит следующее:
{{ define "nav" }}
<nav class="nav-container">
<h1>{{ .path }}</h1>
</nav>
{{ end }}
index.html содержит следующее:
{{ define "index" }}
{{ template "nav" }} <!-- Includes the nav.html file -->
<h1>Welcome to my website. You are visiting {{ .path }}.</h1>
{{ end }}
Я использую пакет template Голанга, а также Martini, что в данном случае не слишком важно.
Моего файл main.go содержит:
package main
import (
"net/http"
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
)
func main() {
m := martiniSetup()
m.Get("/", func(res http.ResponseWriter, req *http.Request, ren render.Render, params martini.Params) {
parse := make(map[string]interface{})
parse["path"] = req.URL.Path
ren.HTML(http.StatusOK, "index", parse)
})
m.Run()
}
Моей проблемы:
.path
переменных разбираемой в index
шаблона, доступный только сами index
шаблона.
Включая шаблон nav
, используя {{ template "nav" }}
внутри index.html
. Проблема заключается в том, что nav.html
не может получить доступ к переменной .path. Он доступен только по шаблону индекса.
Есть ли способ сделать переменную .path
доступной ко всем включенным файлам шаблонов, в моем случае index.html
и nav.html
?
спасибо! Это сработало! :) – Acidic