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 }}.
|
<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,19 +60,29 @@ public class RecursiveResolver : ITemplateResolver
|
||||||
|
|
||||||
public Template GetTemplateByPath(string templatePath)
|
public Template GetTemplateByPath(string templatePath)
|
||||||
{
|
{
|
||||||
RecursiveResolver resolver;
|
RecursiveResolver resolver = templatePath.StartsWith(System.IO.Path.DirectorySeparatorChar) ? Root : this;
|
||||||
Span<string> pathElements = templatePath.Split(System.IO.Path.DirectorySeparatorChar);
|
|
||||||
if (string.Empty.Equals(pathElements[0]))
|
do
|
||||||
resolver = Root;
|
{
|
||||||
else
|
string templateFileName = System.IO.Path.Join(resolver.Path, templatePath);
|
||||||
resolver = FindResolver(pathElements.Slice(0, pathElements.Length - 1));
|
if (File.Exists(templateFileName))
|
||||||
|
{
|
||||||
Template template = resolver?.GetTemplate(pathElements[^1]);
|
if (!_templates.TryGetValue(templateFileName, out Template template))
|
||||||
if (template is null)
|
{
|
||||||
return null;
|
template = new Template(templateFileName, this);
|
||||||
|
_templates.Add(templateFileName, template);
|
||||||
template.Resolver = this;
|
}
|
||||||
return template;
|
|
||||||
|
return template;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (resolver.Parent is null)
|
||||||
|
break;
|
||||||
|
|
||||||
|
resolver = resolver.Parent;
|
||||||
|
} while (true);
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Template GetTemplate(string name)
|
public Template GetTemplate(string name)
|
||||||
|
@ -80,10 +90,10 @@ public class RecursiveResolver : ITemplateResolver
|
||||||
string filename = System.IO.Path.Combine(Path, name);
|
string filename = System.IO.Path.Combine(Path, name);
|
||||||
if (File.Exists(filename))
|
if (File.Exists(filename))
|
||||||
{
|
{
|
||||||
if (!_templates.TryGetValue(name, out Template template))
|
if (!_templates.TryGetValue(filename, out Template template))
|
||||||
{
|
{
|
||||||
template = new Template(filename, this);
|
template = new Template(filename, this);
|
||||||
_templates.Add(name, template);
|
_templates.Add(filename, template);
|
||||||
}
|
}
|
||||||
|
|
||||||
return template;
|
return template;
|
||||||
|
|
Loading…
Reference in New Issue