From c764eed81d9b670c6ba0a0f748d4596e94b5d203 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Sun, 19 Jul 2026 15:35:09 -0400 Subject: [PATCH] Restore include_template tag (#47) This works with Fluid, and there is no reason to mandate a change; we will document the different behavior with include --- src/MyWebLog/Template.fs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/MyWebLog/Template.fs b/src/MyWebLog/Template.fs index d4d3c43..3ed3eca 100644 --- a/src/MyWebLog/Template.fs +++ b/src/MyWebLog/Template.fs @@ -316,10 +316,14 @@ open MyWebLog.Data module Cache = open System.Collections.Concurrent + open System.Text.RegularExpressions /// Cache of parsed templates let private _cache = ConcurrentDictionary () + /// Custom include parameter pattern + let private hasInclude = Regex("""{% include_template \"(.*)\" %}""", RegexOptions.None, TimeSpan.FromSeconds 2L) + /// Get a template for the given theme and template name /// The ID of the theme for which a template should be retrieved /// The name of the template to retrieve @@ -336,8 +340,25 @@ module Cache = | Some theme -> match theme.Templates |> List.tryFind (fun t -> t.Name = templateName) with | Some template -> - _cache[templatePath] <- parser.Parse(template.Text) - return Ok _cache[templatePath] + let mutable text = template.Text + let mutable childNotFound = "" + while hasInclude.IsMatch text do + let child = hasInclude.Match text + let childText = + match theme.Templates |> List.tryFind (fun t -> t.Name = child.Groups[1].Value) with + | Some childTemplate -> childTemplate.Text + | None -> + childNotFound <- + if childNotFound = "" then child.Groups[1].Value + else $"{childNotFound}; {child.Groups[1].Value}" + "" + text <- text.Replace(child.Value, childText) + if childNotFound <> "" then + let s = if childNotFound.IndexOf ";" >= 0 then "s" else "" + return Error $"Could not find the child template{s} {childNotFound} required by {templateName}" + else + _cache[templatePath] <- parser.Parse(text) + return Ok _cache[templatePath] | None -> return Error $"Theme ID {themeId} does not have a template named {templateName}" | None -> return Error $"Theme ID {themeId} does not exist"