Added templates and some notes
parent
43b7e2d986
commit
f0da255c6a
@ -0,0 +1,50 @@
|
||||
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
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
{{define "title"}}Snippet #{{.Snippet.ID}}{{end}}
|
||||
|
||||
{{define "main"}}
|
||||
<!-- This is a comment that will be stripped out by html/template -->
|
||||
<div class="snippet">
|
||||
<div class="metadata">
|
||||
<strong>{{.Snippet.Title.String }}</strong>
|
||||
<span>#{{.Snippet.ID}}</span>
|
||||
</div>
|
||||
<pre><code>{{.Snippet.Content.String }}</code></pre>
|
||||
<div class="metadata">
|
||||
<time>Created: {{.Snippet.CreatedAt}}</time>
|
||||
<time>Expires: {{.Snippet.ExpiresAt}}</time>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
|
Loading…
Reference in New Issue