forked from goffee/cup
Custom html/template FuncMap extensions that bring Liquid-like expressiveness to Go server-side templates. Add template demo.
This commit is contained in:
parent
7bf28afdbf
commit
a7310b24d5
5 changed files with 610 additions and 0 deletions
|
|
@ -9,6 +9,7 @@ import (
|
|||
"math/rand/v2"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"git.smarteching.com/goffee/core"
|
||||
"git.smarteching.com/goffee/core/template/components"
|
||||
|
|
@ -861,3 +862,133 @@ func getPaginatedPageViews(values [][]components.ContentTableTD, limit int, offs
|
|||
return values[offset:end] // Slice the array
|
||||
|
||||
}
|
||||
|
||||
// Custom Templates functions
|
||||
func TemplatesFunctions(c *core.Context) *core.Response {
|
||||
|
||||
// check if template engine is enabled
|
||||
TemplateEnableStr := os.Getenv("TEMPLATE_ENABLE")
|
||||
if TemplateEnableStr == "" {
|
||||
TemplateEnableStr = "false"
|
||||
}
|
||||
TemplateEnable, _ := strconv.ParseBool(TemplateEnableStr)
|
||||
|
||||
if TemplateEnable {
|
||||
|
||||
tmplData := SamplePageData()
|
||||
return c.Response.Template("custom_templates_functions.html", tmplData)
|
||||
|
||||
} else {
|
||||
|
||||
message := "{\"message\": \"Error, template not enabled\"}"
|
||||
return c.Response.Json(message)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Author represents the writer of an article to show the custom functions
|
||||
type Author struct {
|
||||
Name string
|
||||
Avatar string
|
||||
Bio string
|
||||
}
|
||||
|
||||
// Article represents a blog post or news entry used to test template helpers to show the custom functions
|
||||
type Article struct {
|
||||
Title string
|
||||
Slug string
|
||||
Excerpt string
|
||||
Body string
|
||||
Tags []string
|
||||
PublishedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
Author Author
|
||||
Views int
|
||||
Price float64
|
||||
Featured bool
|
||||
Subtitle string // intentionally left empty on some entries to test coalesce/defaultVal
|
||||
}
|
||||
|
||||
// PageData is the top-level context passed to the HTML template to show the custom functions
|
||||
type PageData struct {
|
||||
SiteTitle string
|
||||
Articles []Article
|
||||
}
|
||||
|
||||
// SamplePageData returns a populated PageData ready to be rendered by the template.
|
||||
func SamplePageData() PageData {
|
||||
return PageData{
|
||||
SiteTitle: "go/template lab",
|
||||
Articles: []Article{
|
||||
{
|
||||
Title: "getting started with go templates",
|
||||
Slug: "getting-started-go-templates",
|
||||
Excerpt: "Go's html/template package is both powerful and safe by default. In this article we explore how to extend it with custom FuncMap helpers that bring it closer to the expressiveness of Liquid or Twig, without sacrificing any of the security guarantees.",
|
||||
Tags: []string{"go", "templates", "web", "backend"},
|
||||
PublishedAt: time.Now().Add(-3 * 24 * time.Hour),
|
||||
UpdatedAt: time.Now().Add(-1 * 24 * time.Hour),
|
||||
Views: 14200,
|
||||
Price: 0,
|
||||
Featured: true,
|
||||
Subtitle: "",
|
||||
Author: Author{
|
||||
Name: "marina voss",
|
||||
Avatar: "MV",
|
||||
Bio: "Senior backend engineer focused on developer tooling and observability.",
|
||||
},
|
||||
},
|
||||
{
|
||||
Title: "building a blog engine in go",
|
||||
Slug: "blog-engine-go",
|
||||
Excerpt: "We walk through building a minimal but complete blog engine using only the Go standard library: routing with net/http, persistence with database/sql, and rendering with html/template.",
|
||||
Tags: []string{"go", "blog", "sqlite"},
|
||||
PublishedAt: time.Now().Add(-10 * 24 * time.Hour),
|
||||
UpdatedAt: time.Now().Add(-10 * 24 * time.Hour),
|
||||
Views: 8750,
|
||||
Price: 9.99,
|
||||
Featured: false,
|
||||
Subtitle: "A zero-dependency approach",
|
||||
Author: Author{
|
||||
Name: "rafael okonkwo",
|
||||
Avatar: "RO",
|
||||
Bio: "Full-stack engineer and open-source contributor. Writes about Go and distributed systems.",
|
||||
},
|
||||
},
|
||||
{
|
||||
Title: "concurrency patterns you should know",
|
||||
Slug: "concurrency-patterns-go",
|
||||
Excerpt: "Goroutines are cheap, but misusing them is expensive. This deep-dive covers fan-out/fan-in, pipelines, semaphores, and error group patterns with real production examples.",
|
||||
Tags: []string{"go", "concurrency", "goroutines", "advanced"},
|
||||
PublishedAt: time.Now().Add(-45 * 24 * time.Hour),
|
||||
UpdatedAt: time.Now().Add(-40 * 24 * time.Hour),
|
||||
Views: 31400,
|
||||
Price: 14.99,
|
||||
Featured: true,
|
||||
Subtitle: "",
|
||||
Author: Author{
|
||||
Name: "selin çelik",
|
||||
Avatar: "SÇ",
|
||||
Bio: "Systems programmer. Previously at Cloudflare. Loves writing about the internals of things.",
|
||||
},
|
||||
},
|
||||
{
|
||||
Title: "understanding go interfaces",
|
||||
Slug: "understanding-go-interfaces",
|
||||
Excerpt: "Interfaces in Go are implicit and structural, which makes them both elegant and occasionally surprising. We look at how the runtime dispatches method calls and how to design composable interfaces.",
|
||||
Tags: []string{"go", "interfaces", "design"},
|
||||
PublishedAt: time.Now().Add(-2 * time.Hour),
|
||||
UpdatedAt: time.Now().Add(-1 * time.Hour),
|
||||
Views: 420,
|
||||
Price: 0,
|
||||
Featured: false,
|
||||
Subtitle: "Implicit, structural, and powerful",
|
||||
Author: Author{
|
||||
Name: "marina voss",
|
||||
Avatar: "MV",
|
||||
Bio: "Senior backend engineer focused on developer tooling and observability.",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue