sunng87 / handlebars-rust

Rust templating with Handlebars

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

Thank you, I got it to work. :) My working solution can be found here:

kpcyrd/sh4d0wup@8004fe6

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!