我正在嘗試構建一個小型 Web 應用程序,并且希望將所有 CSS 文件放在一個文件夾中,并讓它們在所有網頁上自動加載(有點像 Rails 資產管道)。我正在使用它來提供 css 文件,但是如何讓它們加載所有頁面?http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("/css/"))))
2 回答

青春有我
TA貢獻1784條經驗 獲得超8個贊
一種解決方案是利用 html/template 功能,創建所有頁面以包含如下所示的相同部分。但是,我會通過在您的每個頁面中留下 來為您的頭腦添加標簽。
{{define "page_template"}}
<head>
<title>My page template</title>
{{template "template_css"}}
<!-- page specific css if required -->
<link rel="stylesheet" type="text/css" href="/assets/additional.css" />
</head>
... etc ...
和 template_css:
{{define "template_css"}}
<link rel="stylesheet" type="text/css" href="/assets/allpages.css" />
{{end}}
模板解析的一段代碼
tp, err := template.ParseFiles("page_template.html", "template_css.tp")
err = tp.ExecuteTemplate(buf, "page_template", templateParameters)
- 2 回答
- 0 關注
- 298 瀏覽
添加回答
舉報
0/150
提交
取消