Documentation
¶
Overview ¶
Package asset helps using static assets (scripts, stylesheets, other files) prepared by external asset pipelines in Go (Golang) templates. It provides template functions that insert references to (optionally) minified and versioned files.
The idea behind this package is that in some cases creating asset bundles is best left to external tools such as grunt or gulp. The default configuration relies on a presence of a JSON file describing a mapping from original to minified assets. Such file can be prepared e.g. by gulp-rev.
Example manifest file:
{ "js/main.min.js": "js/main.min-da89a0c4.js", "css/style.min.css": "css/style.min-16680603.css" }
Example usage in template:
<head> {{ linktag "css/style.css" }} {{ scripttag "js/main.js" }} <!-- Additional attributes can be passed using an even number of arguments: --> {{ scripttag "js/main.js" "charset" "UTF-8" }} </head> <body> <!-- Inserts URL prefix to avoid hardcoding it --> <img src="{{ static }}/img/logo.jpg"/> </body>
Example initialization:
import ( "github.com/rsniezynski/go-asset-helper" "html/template" ) func main() { static, err := asset.NewStatic("/static/", "/path/to/manifest.json") if err != nil { // Manifest file doesn't exist or is not a valid JSON } tmpl := template.Must(template.ParseFiles("template_name.html")) // Attach helper functions with the template: static.Attach(tmpl) // Alternatively: tmpl.Funcs(static.FuncMap()) // Use minified versions if available: static, err = asset.NewStatic("/static/", "/path/to/manifest.json", asset.WithUseMinified(true)) // Use minified versions and load the manifest file using go-bindata (Asset is a go-bindata class). // The loader is a func(string) ([]byte, error) static, err = asset.NewStatic( "/static/", "/path/to/manifest.json", asset.WithUseMinified(true), asset.WithManifestLoader(Asset), ) // There's also WithMappingBuilder option to create an asset mapper without // using the manifest file. }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithManifestLoader ¶
func WithManifestLoader(load Loader) optionSetter
WithManifestLoader can be used to provide Loader implementation in NewStatic
func WithMappingBuilder ¶
func WithMappingBuilder(builder MappingBuilder) optionSetter
WithMappingBuilder can be used to provide MappingBuilder implementation in NewStatic
func WithUseMinified ¶
func WithUseMinified(minified bool) optionSetter
WithUseMinified can be used in NewStatic to specify whether resoruce mapping should be used. false can be useful in debug mode.
Types ¶
type MappingBuilder ¶
type MappingBuilder func() (StaticMapper, error)
MappingBuilder is a function that produces StaticMapper instances
type Static ¶
type Static struct {
// contains filtered or unexported fields
}
Static holds configurtion for the asset resolver. It should be created using NewStatic
func NewStatic ¶
NewStatic creates an instance of static, which can be then used to attach helper functions to templates.
func (*Static) Attach ¶
Attach sets ScriptTag, LinkTag and Stastic as, respectively, scripttag, linktag and static template functions.
func (*Static) FuncMap ¶
FuncMap returns template.FuncMap that can be used to attach go-asset-helper functions to a template.
func (*Static) LinkTag ¶
LinkTag returns HTML script tag. See ScriptTag for additional information. Usually not used directly, but registered in tempalte via FuncMap.
func (*Static) ScriptTag ¶
ScriptTag returns HTML script tag; path should point to an asset, by default a path on the disk relative to the directory from which the application process is started. This behavior can be modified by providing a different loader on Static object creation. attrs can be used to pass additional attributes to the tag. There must be an even numner of attrs. Usually not used directly, but registered in tempalte via FuncMap.
type StaticMapper ¶
type StaticMapper interface { // Get returns reference to an specified as a path. Get(string) string }
StaticMapper is an interface for mapping between asset paths and references to be put in template tags