Add a Handlebars LibraryTemplateLoader and rework file loading to support partial templates #4921
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Identify the Bug or Feature request
closes #4920
Description of the Change
This has two major changes:
Moves the code for injecting the base tag to after templates have been applied instead of when they are read.
This is necessary because the syntax for including partial templates is
{{> path}}
but the Jsoup code that adds the
<base>
tag normalises this strictly invalid HTML to{{> path}}
which makes it an invalid Handlebars template.
Adds a Handlebars TemplateLoader that loads from lib:// URLs.
Because the templates are already publicly exported from add-ons,
and they can load css from other files with relative URLs
it is natural to support loading partial templates the same way.
It is implemented as a URL loader which is aware of the path of the template being loaded
and interprets relative paths as relative to the directory.
As-implemented, absolute paths can load content from other add-ons, but this could be prevented by adjusting the prefix and current paths.
See https://gitlab.com/richardmaw/maptool-bar-test/-/tree/main/mtlib/library/public/sheets/basic for an example stat sheet that uses partial templates.
Possible Drawbacks
lib:// URIs were used to implement it as a URLTemplateLoader because handlebars.java already had a URL-based loader
and it was less intrusive to pass the URL around.
It would instead be possible to define a loader that uses a reference to the library which would have the benefit of allowing non-public templates, and bypasses the URL fetch machinery, but it may be useful to permit add-ons to depend on each other.
Documentation Notes
In https://wiki.rptools.info/index.php/Stat_Sheet_Tutorial there is a comment:
Which was probably due to the base tag being added before templates are parsed.
Release Notes
{{> path}}
syntax, which includes the contents of the named partial template file in the current template. If the path is an absolute path the first component is the library namespace. If the path is a relative path it is in the same directory as the template.include
helper, e.g.{{include "foo" bar=baz}}
will include the partial template"foo"
with the context altered to set bar to the value of baz in the current context.This change is