register-dynamics / data-import

Components and patterns for building spreadsheet uploaders in digital services
https://register-dynamics.github.io/data-import/
1 stars 0 forks source link

Viewing templates before creating them doesn't work #63

Open simonwo opened 2 weeks ago

simonwo commented 2 weeks ago

The Prototype Kit includes a "Templates" page when run in development mode that allows the designer to create pages from templates exposed by plugins, or to view them before creating them.

In testing we observed designers using the View feature, but in our plugin viewing templates doesn't work. This is because macros defined in plugin code don't get exported to template viewing but they do in pages.

We can redefine some of our exported functions as pure macros, but not all of them due to limitations with Nunjucks. So we need to come up with some way to get functions loaded during template viewing (including possibly submitting a patch to the Prototype Kit).

rossjones commented 2 weeks ago

Currently we define these functions in the plugin for use in the prototype by passing in a reference to the prototype kit in our initialisation call. Unfortunately, the prototype kit seems designed for people to add functions within the prototype itself and not from plugins. Mentions of plugin config key nunjucksFunctions seem to load functions from the app's functions.js file, rather than the plugins.

The plugin does not currently have a dependency on the prototype kit, because we don't want the circular reference, making it impossible to add a function at the global level in the plugin unless the manage-plugin pages call our initialise function.

We might be able to extend the prototype kit to optionally locate/call a specific function to initialise the plugin, in much the same way that other plugin systems dlsym a function to return a structure of pointers to functions. We could discover a specific function signature and call it with config and reference to the library etc. Whether we can get this merged in a reasonable time-frame is another question.

rossjones commented 1 week ago

Had a look at trying to wrap the main content in the templates with a check for the existence of a specific function, and it's fine, it hides the existing content. Unfortunately, if we have an else block to show some static content when previewing the page then:

It's also probably true that there'll be a problem if you install a template before you've added the initialisation code as the functions won't be available.