英文:
Loading CSS files from a folder in Go
问题
我正在尝试构建一个小型的Web应用程序,我希望将所有的CSS文件放在一个文件夹中,并且让它们在所有的网页上自动加载(类似于Rails的asset pipeline)。
我正在使用以下代码来提供CSS文件的服务,但是如何让它们在所有页面上加载呢?
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("/css/"))))
英文:
I'm trying to build a small web app, and I'd like to have all my CSS files in one folder, and have them load automatically on all web pages (sort of like the Rails asset pipeline).
I'm using this to serve the css files, but how would I get them to load with all pages?
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("/css/"))))
答案1
得分: 2
一种解决方案是利用html/template功能,创建包含相同<head>部分的所有页面,如下所示。但是,我建议在每个页面中保留<head></head>,以便添加标签到头部。
{{define "page_template"}}
<head>
<title>我的页面模板</title>
{{template "template_css"}}
<!-- 如果需要,可以添加特定于页面的CSS -->
<link rel="stylesheet" type="text/css" href="/assets/additional.css" />
</head>
... 其他内容 ...
还有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)
以上是要翻译的内容。
英文:
One solution is to make use of the html/template functionality, create all your pages to include the same <head> section like below. I would however leave room to add tags to your head by leaving the <head></head> in each of your pages.
{{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 ...
And the template_css:
{{define "template_css"}}
<link rel="stylesheet" type="text/css" href="/assets/allpages.css" />
{{end}}
A snippet of code for the template parsing
tp, err := template.ParseFiles("page_template.html", "template_css.tp")
err = tp.ExecuteTemplate(buf, "page_template", templateParameters)
答案2
得分: 2
我认为实现这个简单的资产管道功能很容易,你可以使用path.filepath来遍历你的css目录,读取所有的css文件,通过将所有行连接在一起生成一个临时的css文件,然后将生成的文件提供给客户端。
import (
"path/filepath"
"os"
"io/ioutil"
)
func Generate(path string) *os.File{
f,err := ioutil.TempFile("","all")
if err!=nil{
return nil
}
filepath.Walk(path,func(p string,info os.FileInfo,err error)error{
if err!=nil{
return err
}
if !info.IsDir(){
data,err := ioutil.ReadFile(info.Name())
if err!=nil{
return err
}
f.Write(data)
}
return err
})
return f
}
英文:
I think it's easy to implement this simple asset pipeline feature, you can use path.filepath to walk through your css directory, read all the css files, generate a temp css file by join all lines together, then serve the client with the generated file
import (
"path/filepath"
"os"
"io/ioutil"
)
func Generate(path string) *os.File{
f,err := ioutil.TempFile("","all")
if err!=nil{
return nil
}
filepath.Walk(path,func(p string,info os.FileInfo,err error)error{
if err!=nil{
return err
}
if !info.IsDir(){
data,err := ioutil.ReadFile(info.Name())
if err!=nil{
return err
}
f.Write(data)
}
return err
})
return f
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论