RPTools / maptool

Virtual Tabletop for playing roleplaying games with remote players or face to face.
http://rptools.net
GNU Affero General Public License v3.0
800 stars 262 forks source link

Add a Handlebars LibraryTemplateLoader and rework file loading to support partial templates #4921

Closed fishface60 closed 2 months ago

fishface60 commented 2 months ago

Identify the Bug or Feature request

closes https://github.com/RPTools/maptool/issues/4920

Description of the Change

This has two major changes:

  1. 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 {{&gt; path}} which makes it an invalid Handlebars template.

  2. 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:

The & CSS selector didn't work when I created the examples, but may be available by the time you read this. It doesn't hurt to try it and see.

Which was probably due to the base tag being added before templates are parsed.

Release Notes


This change is Reviewable