You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.5 KiB
Go
51 lines
1.5 KiB
Go
package server
|
|
|
|
import (
|
|
"database/sql"
|
|
"html/template"
|
|
|
|
"git.runcible.io/learning/ratchet/internal/model"
|
|
)
|
|
|
|
// Define a templateData type to act as the holding structure for
|
|
// any dynamic data that we want to pass to our HTML templates.
|
|
// At the moment it only contains one field, but we'll add more
|
|
// to it as the build progresses.
|
|
type templateData struct {
|
|
Snippet model.Snippet
|
|
}
|
|
|
|
// TEMPLATE FILTERS
|
|
|
|
var templateFuncMap = template.FuncMap{
|
|
// This is a trivial example because you can simply use {{ .Content.String }} to call the sql.NullString.String() function.
|
|
"nullStringToStr": func(ns sql.NullString) string {
|
|
if ns.Valid {
|
|
return ns.String
|
|
}
|
|
return ""
|
|
},
|
|
}
|
|
|
|
// parseTemplateFiles parses the provided template files and extends the resulting
|
|
// template with additional custom template functions (filters) defined in the
|
|
// templateFuncMap.
|
|
//
|
|
// This function serves as a wrapper around template.ParseFiles, allowing the
|
|
// inclusion of reusable template functions to enhance the template's capabilities.
|
|
//
|
|
// Parameters:
|
|
// - files: A variadic list of file paths to the template files to be parsed.
|
|
//
|
|
// Returns:
|
|
// - (*template.Template): The parsed template with custom functions injected.
|
|
// - (error): An error if the template files cannot be parsed.
|
|
func parseTemplateFiles(files ...string) (*template.Template, error) {
|
|
tmpl := template.New("").Funcs(templateFuncMap)
|
|
tmpl, err := tmpl.ParseFiles(files...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return tmpl, nil
|
|
}
|