mirror of
https://github.com/sbrow/thor.git
synced 2026-08-26 11:23:32 -04:00
feat: Improved the template chain.
This commit is contained in:
+24
-10
@@ -120,24 +120,38 @@ get_template :: proc(
|
|||||||
layout: string,
|
layout: string,
|
||||||
cache: ^map[string]mustache.Template,
|
cache: ^map[string]mustache.Template,
|
||||||
) -> mustache.Template {
|
) -> mustache.Template {
|
||||||
if cached, ok := cache[layout]; ok {
|
// Build fallback chain: <layout> → [section_index] → page → base
|
||||||
|
chain: [4]string
|
||||||
|
n := 0
|
||||||
|
chain[n] = layout; n += 1
|
||||||
|
if strings.has_suffix(layout, "_index") && layout != "section_index" {
|
||||||
|
chain[n] = "section_index"; n += 1
|
||||||
|
}
|
||||||
|
if layout != "page" && layout != "base" {
|
||||||
|
chain[n] = "page"; n += 1
|
||||||
|
}
|
||||||
|
if layout != "base" {
|
||||||
|
chain[n] = "base"; n += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
for i in 0..<n {
|
||||||
|
candidate := chain[i]
|
||||||
|
if cached, ok := cache[candidate]; ok {
|
||||||
return cached
|
return cached
|
||||||
}
|
}
|
||||||
|
filename := fmt.tprintf("%s.html", candidate)
|
||||||
filename := fmt.tprintf("%s.html", layout)
|
|
||||||
if os.exists(fmt.tprintf("%s/%s", layouts_dir, filename)) {
|
if os.exists(fmt.tprintf("%s/%s", layouts_dir, filename)) {
|
||||||
tpl := load_template(layouts_dir, filename)
|
tpl := load_template(layouts_dir, filename)
|
||||||
cache[layout] = tpl
|
cache[candidate] = tpl
|
||||||
return tpl
|
return tpl
|
||||||
}
|
}
|
||||||
|
if candidate != chain[n - 1] {
|
||||||
if layout != "page" {
|
log.warnf("thor: template %s not found, falling back", filename)
|
||||||
log.warnf("thor: template %s not found, falling back to page.html", filename)
|
}
|
||||||
return get_template(layouts_dir, "page", cache)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.errorf("thor: default template page.html not found in %s", layouts_dir)
|
log.errorf("thor: base.html not found in %s", layouts_dir)
|
||||||
return load_template(layouts_dir, "page.html")
|
return mustache.Template{}
|
||||||
}
|
}
|
||||||
|
|
||||||
capitalize :: proc(s: string) -> string {
|
capitalize :: proc(s: string) -> string {
|
||||||
|
|||||||
Reference in New Issue
Block a user