Skip to content

Loading…

Add dust.exists() function to see if template has been cached (w/ implementation) #209

Closed
jbcpollak opened this Issue · 8 comments

4 participants

@jbcpollak

I have added this function to my branch of dust, I use to to know if a template exists before I call render():

dust.exists = function(name) {
var tmpl = dust.cache[name];
if (tmpl) {
return true;
} else {
return false;
}
};

Perhaps this isn't necessary in most cases, I have been using dust through Rhino and it seemed like the best solution at the time. Anyway, it seems like a useful function to have.

@jxp
jxp commented

This seems like it would definitely be useful for async coding where you want to be sure templates have loaded before using them

@vybs

Not sure how much value this adds.

@vybs

@jimmyhchan @rragan can we close this?

@rragan

Seems like a very special use case. I don't see a general need. Since code is available in the issue others can use as needed.

@vybs

closing it.

@vybs vybs closed this
@jbcpollak

I'm ok with closing this (although a bit disappointed :), but I wanted to explain the use-case for posterity, in case anyone else has a similar issue:

The use-case is actually pretty simple. We have multiple rendering solutions, with dust being the new preferred solution, but lots of old templates are processed with an older renderer. For simplicity, components will call an abstraction layer and just execute templateRenderer.render("my.template.key"). The TemplateRenderer calls the dustRenderer.exist() function, and if it returns false, falls back down the chain of other renderers.

This allows us to move a template from one rendering engine to another without having to change any other data structures or code.

Anyway, I hope that explains the use-case, I probably should have mentioned it when I opened the ticket.

@vybs

what does the templateRenderer.render("my.template.key"). look like?
You mention using rhino.

why cannot that catch the error from the dust.js and then return false?

@jbcpollak

Good questions. The render() just returns a HTML partial as a string. In this specific case it returns the rendered HTML body of an email, but I don't think it matters.

I suppose I could catch the error from dust.js, or Rhino, if it throws an exception, but this seemed a bit cleaner, and helps proactively know the difference between a template rendering failure (which I would handle differently) and a missing template.

Is there a way to distinguish between a dust.render() error caused by a missing template and a template rendering error? If that is possible I'd have to figure out how to interpret that in Rhino, but I'm sure that is do-able.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.