sunng87 / handlebars-rust

Rust templating with Handlebars

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rendering a template requires it to live for as long as the registry

lovasoa opened this issue · comments

Currently the types in this crate allow Template objects to live outside of the registry, but the lifetime annotations of the render method make it very hard to actually implement a system like that.

Is there a reason why a template that lives shorter than the registry where partials are stored couldn't be rendered?

I would need that for sqlpage

IIRC there is no particular reason for that just because it was assumed that all templates are managed by the registry. Do you have an example that I can work on to verify?

For now you can use render_template to render it directly from string with a little cost of parsing. Actually in render_template's implementation, template lives short than the registry.

I need this because I am handling my own customized template cache, so re-parsing the templates every time would make the whole thing useless. Would it be hard to remove the 'reg lifetime on self in Renderable::render ?

It should not be hard. But it can be a break change by introducing additional lifetime specifier. I'm going to do some experiment to see if it works for your case.

Thank you very much ! Don't hesitate to share your progress so that I can test it in SQLPage.
And do we need to add a new lifetime ? Couldn't we have a simpler signature like

    fn render<'reg: 'rc, 'rc>(
-        &'reg self,
+        &self,
        registry: &'reg Registry<'reg>,
        ctx: &'rc Context,
        rc: &mut RenderContext<'reg, 'rc>,
        out: &mut dyn Output,
    ) -> Result<(), RenderError>

Removing lifetime won't work because some constants from template are passed within rendering call chain, which requires explicit lifetime. The currently design assumes all templates are stored within registry so data in templates shares same lifetime of registry.

An alternative approach for your problem is to create multiple registries and manage them with your strategy.

I do need a single registry, with all the associated helpers, but need to store templates in my own data structure. Creating a new registry with all the associated helpers on every render would be a huge overhead !
Unsafely transmuting the lifetimes just works and doesn't cause any memory issue, which seems to indicate that it is indeed possible to relax the lifetime requirements, at least in my case. Does the registry actually store borrowed information from the template being rendered ? If not, the lifetime requirement is too strict.

Would it be possible to at least have the requirement that the template lives longer than the context 'rc, but not the registry 'reg ?