javascript - Handlebars Failover Content With Lookup Helper -




i looking render handlebars partial via lookup helper. isn't problem, can code:

{{> (lookup . 'type') }} 

however, if variable type doesn't render value in partials directory, 500 error throws. handlebars supports failover content. such as:

{{#> mypartial }}   failover content {{/mypartial}} 

my question is, can combine lookup failover?

i hoping like:

{{#> (lookup . 'type') }} failover content {{/(lookup . 'type')}} 

i don't think there way combine dynamic lookup helper partial block. however, think can come custom helper job us.

all our helper needs take partial name , try find partial name in handlbars.partials map. if finds partial, return result of calling partial template current data context. otherwise, our helper return template within block (the failover content).

handlebars.registerhelper('partialresolver', function (partialname, options) {     var partial = handlebars.partials[partialname];      if (partial && !handlebars.utils.isfunction(partial)) {         handlebars.partials[partialname] = partial = handlebars.compile(partial);     }      return (partial ? partial : options.fn)(this); }); 

note: condition in middle required because partials can registered either strings (which compiled on demand) or pre-compiled templates. if partial string, compile , assign handlebars.partials[partialname].

i have created example fiddle reference.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -