Lazy initialize a variable
kpcyrd opened this issue · comments
hello!
I currently have templates like this:
/foo/{{sha256}}
and I calculate this hash (and a few others) in advance. Now, for performance reasons I don't want to calculate these values in advance anymore and instead have a sha256: Option<String>
that gets lazy initialized on access, so only the hashes that are used are calculated, but only once.
Is there any documentation on how to do this?
Hi @kpcyrd , you can make sha256 a helper so it calls some rust code when rendering
Is there an example somewhere? :) I'm having trouble creating a helper that works with no arguments.
See https://github.com/sunng87/handlebars-rust/blob/master/tests/data_helper.rs
It should be similar as normal helper.
Thank you, I got it to work. :) My working solution can be found here:
The only difference where it doesn't behave like a "lazy variable" was when combining it with another helper. Instead of:
{{slice-until sha1 2}}
I had to write:
{{slice-until (sha1) 2}}
I hope this thread is useful to others. Cheers!