Documentation ¶
Overview ¶
Package templates handles parsing and rendering HTML. This more-or-less wraps the golang html/template package with some tooling for storing the parsed templates, showing a requested template, and using source HTML stored in on-disk or embedded files.
Handling of HTML templates is done by parsing files in given directories and caching them for future use within your application. Templates can be stored in numerous subdirectories for ease of organization and allowing the same filename or template declaration ({{define}}) to be used. Files stored at the root templates directory are inherited into each subdirectory; this is useful for storing files with shared {{declare}} blocks that are imported into other templates stored in numerous subdirectories (ex.: header and footers).
Serving of a template is done by providing the subdirectory and name of the template (aka filename). Note that due to this, you cannot serve templates from the root directory. Again, the root directory is for storing templates shared templates between multiple subdirectories.
An example of a directory structure for storing templates is below. templates/ ├─ header.html ├─ footer.html ├─ docs/ │ ├─ index.html │ ├─ faq.html │ ├─ how-to.html ├─ app/ │ ├─ index.html │ ├─ users.html │ ├─ widgits.html
Index ¶
- Variables
- func Build() (err error)
- func CacheBustingFilePairs(pairs map[string]string)
- func Debug(yes bool)
- func DefaultConfig()
- func DefaultEmbeddedConfig(embeddedFS embed.FS, basePath string, subdirs []string)
- func DefaultFuncMap() template.FuncMap
- func DefaultOnDiskConfig(basePath string, subdirs []string)
- func Development(yes bool)
- func FuncAddInt(x interface{}, y int) (z int)
- func FuncDateReformat(date, format string) (d string)
- func FuncIndexOf(needle, haystack string) int
- func PrintEmbeddedFileList(e embed.FS)
- func Show(w http.ResponseWriter, subdir, templateName string, injectedData interface{})
- func UseLocalFiles(yes bool)
- type Config
Constants ¶
This section is empty.
Variables ¶
var ( //ErrBasePathNotSet is returned if a user calls Save() and not path to the //templates was provided. ErrBasePathNotSet = errors.New("templates: no value set for TemplatesBasePath") //ErrNoSubDirsProvided is returned when no subdirectories were provided. As of //now we require at least one subdirectory. ErrNoSubDirsProvided = errors.New("templates: no template subdirectories were provided, at least one must be") //ErrInvalidSubDir is returned if a user calls Save() and the provided //subdirectory cannot be found. ErrInvalidSubDir = errors.New("templates: empty or all whitespace string provided for TemplatesSubDirs is not allowed") //ErrNoEmbeddedFilesProvided is returned when a user is using a config with embedded files //but no embedded files were provided. ErrNoEmbeddedFilesProvided = errors.New("templates: no embedded files provided") )
errors
Functions ¶
func Build ¶
func Build() (err error)
Build builds the templates using the default package level config.
func CacheBustingFilePairs ¶
CacheBustingFilePairs sets the CacheBustingFilePairs field on the package level config.
func Debug ¶ added in v1.1.0
func Debug(yes bool)
Debug sets the Development field on the package level config.
func DefaultConfig ¶
func DefaultConfig()
DefaultConfig initializes the package level config with some defaults set. This wraps NewConfig() and saves the config to the package.
func DefaultEmbeddedConfig ¶
DefaultEmbeddedConfig initializes the package level config with the path and directories provided and some defaults.
func DefaultFuncMap ¶
DefaultFuncMap returns the list of extra funcs defined for use in templates.
func DefaultOnDiskConfig ¶
DefaultOnDiskConfig initializes the package level config with the path and directories provided and some defaults.
func Development ¶
func Development(yes bool)
Development sets the Development field on the package level config.
func FuncDateReformat ¶
FuncDateReformat is used to transform a date from the yyyy-mm-dd format to another format in templates.
func FuncIndexOf ¶
FuncIndexOf returns the position of needle in haystack. If needle does not exist in haystack, -1 is returned.
func PrintEmbeddedFileList ¶
PrintEmbeddedFileList prints out the list of files embedded into the executable.
This should only be used for diagnostics purposes only to confirm which files are embedded with the //go:embed directives elsewhere in your app.
func Show ¶
func Show(w http.ResponseWriter, subdir, templateName string, injectedData interface{})
Show handles showing a template using the default package-level config.
func UseLocalFiles ¶
func UseLocalFiles(yes bool)
UseLocalFiles sets the UseLocalFiles field on the package level config.
Types ¶
type Config ¶
type Config struct { //Development is passed to each template when rendering the HTML to be sent to //the user so that the HTML can be altered based on if you are running your app //in a development mode/enviroment. Typically this is used to show a banner on //the page, loads extra diagnostic libraries or tools, and uses non-cache busted //static files. Development bool //UseLocalFiles is passed to each template when rendering the HTML to be sent to //the user so that the HTML can be altered to use locally hosted third party //libraries (JS, CSS) versus libraries retrieve from the internet. UseLocalFiles bool //BasePath is the absolute path to the root directory where template files are //stored. There should be at least one template file at this path. This path can //include a single level of subdirectories that contain templates. // //For embedded files (using the embed package), this path does not start with a //foward slash, is forward slash separated, and must be included via //go:embed //directives. See https://pkg.go.dev/embed for more information. BasePath string //SubDirs is a list of subdirectories off of the BasePath where you store template //files. This can be empty if you have no subdirectories. This must only be the //actual subdirectory names, not full paths; full paths will be constructed by //appending each SubDir to the BasePath. SubDirs []string //Extension is the extension you use for your template files. The defaults to "html". Extension string //UseEmbedded means files built into the golang executable will be used rather //than files stored on-disk. You must have read the embedded files, with code //such as var embeddedFiles embed.FS, prior and you must provide the embed.FS to //the field EmbeddedFS. UseEmbedded bool //EmbeddedFiles is the filesystem embedded into this executable via the embed //package. You must have read the embedded files, with code such as //`var embeddedFiles embed.FS` prior to using this package and you must set //UseEmbedded to true to enable use of these files. EmbeddedFS embed.FS //FuncMap is a collection of functions that you want to use in your templates to //augment the golang provided templating funcs. This package provides some default //extra funcs in templates-templatefuncs.go. See https://pkg.go.dev/text/template for //more info. //To provide extra funcs to templates, use code such as the following: /* config, err := templates..DefaultOnDiskConfig("/path/to/templates", []string{"subdir1", "subdir2"}) if err != nil { //handle err } config.FuncMap = template.FuncMap{ "indexOf": templates.FuncIndexOf, "myNewFunc": myNewFunc, } err = config.Build() if err != nil { //handle err } */ FuncMap template.FuncMap //CacheBustingFilePairs is a key-value list of filesnames that match up an original //file name to the file's cache busting file name. This list is then passed to your //templates when rendered to replace the known original filename (i.e.: script.min.js) //with the cache busting filename that is most likely programmatically created (i.e.: //A1B2C3D4.script.min.js). See the package github.com/c9845/cachebusting for an example //implementation and tooling. // //To use the cache busting file, you would use template code similar to the following //to handle the filename replacement: /* <head> {{$originalFile := "styles.min.css"}} {{$cacheBustFiles := .CacheBustFiles}} {{/*If the key "styles.min.css" exists in $cacheBustFiles, then the associated cache-busted filename will be returned as {{.}}. *\/}} {{with index $cacheBustFiles $originalFile}} {{$cacheBustedFile := .}} <link rel="stylesheet" href="/static/css/{{$cacheBustedFile}}"> {{else}} <link rel="stylesheet" href="/static/css/{{$originalFile}}"> {{end}} </head> */ CacheBustingFilePairs map[string]string //Debug prints out debugging information if true. Debug bool // contains filtered or unexported fields }
Config is the set of configuration settings for working with templates.
func GetConfig ¶
func GetConfig() (c *Config)
GetConfig returns the current state of the package level config.
func NewConfig ¶
func NewConfig() *Config
NewConfig returns a config for managing your templates with some defaults set.
func NewEmbeddedConfig ¶
NewEmbeddedConfig returns a config for managing your templates when the source files are stored embedded in the app executable.
func NewOnDiskConfig ¶
NewOnDiskConfig returns a config for managing your templates when the source files are stored on disk.
func (*Config) Build ¶
Build handles finding the templates files, parsing them, and building the golang templates.
Templates are built for the BasePath and each subdirectory. Templates located at the BasePath are inherited into each SubDir. You can have the same template or file name in each SubDir but not in the BasePath and SubDir.
func (*Config) Show ¶
func (c *Config) Show(w http.ResponseWriter, subdir, filename string, injectedData interface{})
Show renders a template as HTML and writes it to w. This works by taking a subdirectory's name and the name of a template file, looks up the template that was parsed earlier in Build(), and returns it with any injected data available at {{.Data}} in HTML templates.
This does not work with {{defined}}{{end}} templates. Templates must be files. A template can include {{defined}}{{end}} and {{template}}{{end}} blocks.