Fixed Template lookup and caching in RecursiveResolver.cs
parent
6ebd79ac21
commit
dfb3850f36
|
@ -67,3 +67,58 @@ Include content by expressions within text elements:
|
|||
|
||||
<div>This is my favourite color: {{ myFavouriteColor }}.
|
||||
|
||||
### Filesystem overlay
|
||||
|
||||
Overlaying is possible by using RecursiveResolver class as ITemplateResolver.
|
||||
|
||||
Example directory layout:
|
||||
|
||||
/layout/frame.html
|
||||
/layout/htmlhead.html
|
||||
/tmpl_a.html
|
||||
|
||||
The files use the following references:
|
||||
|
||||
tmpl_a.html -> /layout/frame.html -> /layout/htmlhead.html
|
||||
|
||||
htmlhead.html may contain something like
|
||||
|
||||
<head>
|
||||
<title>...</title>
|
||||
<link rel=.... >
|
||||
...
|
||||
</head>
|
||||
|
||||
We can now add a file to new folder:
|
||||
|
||||
/layout/frame.html
|
||||
/layout/htmlhead.html
|
||||
/tmpl_a.html
|
||||
/layouts/b/layout/htmlhead.html
|
||||
|
||||
By a call to RecursiveResolver.FindResolver("/layouts/b") we get a ITemplateResolver
|
||||
that looks up Folders and Templates in that directory first, then in the
|
||||
parent folders back to the root folder of this structure.
|
||||
|
||||
So if we call GetTemplateByPath("tmpl_a.html") we get the same Template
|
||||
but this instance will use the reference chain
|
||||
|
||||
tmpl_a.html -> /layout/frame.html -> /layouts/b/htmlhead.html
|
||||
|
||||
which effectivly overlays *htmlhead.html* with an alternative version and still uses the
|
||||
*/layout/frame.html* template.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -60,30 +60,40 @@ public class RecursiveResolver : ITemplateResolver
|
|||
|
||||
public Template GetTemplateByPath(string templatePath)
|
||||
{
|
||||
RecursiveResolver resolver;
|
||||
Span<string> pathElements = templatePath.Split(System.IO.Path.DirectorySeparatorChar);
|
||||
if (string.Empty.Equals(pathElements[0]))
|
||||
resolver = Root;
|
||||
else
|
||||
resolver = FindResolver(pathElements.Slice(0, pathElements.Length - 1));
|
||||
RecursiveResolver resolver = templatePath.StartsWith(System.IO.Path.DirectorySeparatorChar) ? Root : this;
|
||||
|
||||
Template template = resolver?.GetTemplate(pathElements[^1]);
|
||||
if (template is null)
|
||||
return null;
|
||||
do
|
||||
{
|
||||
string templateFileName = System.IO.Path.Join(resolver.Path, templatePath);
|
||||
if (File.Exists(templateFileName))
|
||||
{
|
||||
if (!_templates.TryGetValue(templateFileName, out Template template))
|
||||
{
|
||||
template = new Template(templateFileName, this);
|
||||
_templates.Add(templateFileName, template);
|
||||
}
|
||||
|
||||
template.Resolver = this;
|
||||
return template;
|
||||
}
|
||||
|
||||
if (resolver.Parent is null)
|
||||
break;
|
||||
|
||||
resolver = resolver.Parent;
|
||||
} while (true);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Template GetTemplate(string name)
|
||||
{
|
||||
string filename = System.IO.Path.Combine(Path, name);
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
if (!_templates.TryGetValue(name, out Template template))
|
||||
if (!_templates.TryGetValue(filename, out Template template))
|
||||
{
|
||||
template = new Template(filename, this);
|
||||
_templates.Add(name, template);
|
||||
_templates.Add(filename, template);
|
||||
}
|
||||
|
||||
return template;
|
||||
|
|
Loading…
Reference in New Issue