从Go中的文件夹加载CSS文件

huangapple go评论73阅读模式
英文:

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 &quot;page_template&quot;}}
&lt;head&gt;
    &lt;title&gt;My page template&lt;/title&gt;
    {{template &quot;template_css&quot;}}
    &lt;!-- page specific css if required --&gt;
    &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;/assets/additional.css&quot; /&gt;
&lt;/head&gt;
... etc ...

And the template_css:

{{define &quot;template_css&quot;}}
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;/assets/allpages.css&quot; /&gt;
{{end}}

A snippet of code for the template parsing

tp, err := template.ParseFiles(&quot;page_template.html&quot;, &quot;template_css.tp&quot;)
err = tp.ExecuteTemplate(buf, &quot;page_template&quot;, 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 (
&quot;path/filepath&quot;
&quot;os&quot;
&quot;io/ioutil&quot;
)

func Generate(path string) *os.File{
f,err := ioutil.TempFile(&quot;&quot;,&quot;all&quot;)
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
}

huangapple
  • 本文由 发表于 2013年11月26日 00:03:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/20197563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定