2

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/"))))
3
  • include them in your output? ._. Commented Nov 25, 2013 at 16:15
  • What do you mean by loading with all pages? The response depends on the client request. Commented Nov 25, 2013 at 16:39
  • As in, I want to be able to use the css styles in the /css/ folder in all of the html pages I'm serving (without having to manually include them in the html) Commented Nov 25, 2013 at 16:42

2 Answers 2

2

One solution is to make use of the html/template functionality, create all your pages to include the same section like below. I would however leave room to add tags to your head by leaving the 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)
Sign up to request clarification or add additional context in comments.

2 Comments

To add to this, it's likely you'll end up defining multiple fragments: head, top, XXXXX_page, footer, etc. and possibly even some fragments that include scripts (that you may not want on all pages. Most of my templates end up having 5-6 "includes" so I can modularise them as much as possible.
While this seems like a good way of doing it, there's still code that has to be repeated on each page. I'm looking for a way to do this without having to include it on each html page (eg. somehow append the {{template "template_css" }} to each html page that I pass to template.ParseFiles())
2

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
}

1 Comment

as it's ok to join css files directly together

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.