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

Closed
jbcpollak opened this Issue Dec 18, 2012 · 12 comments

Comments

Projects
None yet
8 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

This comment has been minimized.

Show comment
Hide comment
@jxp

jxp Feb 1, 2013

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

jxp commented Feb 1, 2013

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

@vybs

This comment has been minimized.

Show comment
Hide comment
@vybs

vybs Feb 21, 2013

Contributor

Not sure how much value this adds.

Contributor

vybs commented Feb 21, 2013

Not sure how much value this adds.

@vybs

This comment has been minimized.

Show comment
Hide comment
@vybs

vybs Feb 21, 2013

Contributor

@jimmyhchan @rragan can we close this?

Contributor

vybs commented Feb 21, 2013

@jimmyhchan @rragan can we close this?

@rragan

This comment has been minimized.

Show comment
Hide comment
@rragan

rragan Feb 21, 2013

Contributor

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.

Contributor

rragan commented Feb 21, 2013

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

This comment has been minimized.

Show comment
Hide comment
@vybs

vybs Feb 21, 2013

Contributor

closing it.

Contributor

vybs commented Feb 21, 2013

closing it.

@vybs vybs closed this Feb 21, 2013

@jbcpollak

This comment has been minimized.

Show comment
Hide comment
@jbcpollak

jbcpollak Feb 21, 2013

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.

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

This comment has been minimized.

Show comment
Hide comment
@vybs

vybs Feb 21, 2013

Contributor

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?

Contributor

vybs commented Feb 21, 2013

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

This comment has been minimized.

Show comment
Hide comment
@jbcpollak

jbcpollak Feb 21, 2013

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.

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.

@tonytopper

This comment has been minimized.

Show comment
Hide comment
@tonytopper

tonytopper Feb 23, 2016

This could be very useful for dynamically named template files.

This could be very useful for dynamically named template files.

@sethkinast

This comment has been minimized.

Show comment
Hide comment
@sethkinast

sethkinast Feb 23, 2016

Collaborator

This logic should likely be part of your dust.onLoad hook.

Collaborator

sethkinast commented Feb 23, 2016

This logic should likely be part of your dust.onLoad hook.

@soldiercnorm

This comment has been minimized.

Show comment
Hide comment
@soldiercnorm

soldiercnorm Mar 2, 2016

This would be nice

This would be nice

@dehimer

This comment has been minimized.

Show comment
Hide comment
@dehimer

dehimer Apr 14, 2016

My use case: check invoice template depending on the current user language.
Show button of invoice if exist template for current language.
I use nodejs and now I decide it by testing existing template file by name "invoicehtml.dust".
Don't beautiful. I except something like this: dust.check("invoicehtml"+lang)

dehimer commented Apr 14, 2016

My use case: check invoice template depending on the current user language.
Show button of invoice if exist template for current language.
I use nodejs and now I decide it by testing existing template file by name "invoicehtml.dust".
Don't beautiful. I except something like this: dust.check("invoicehtml"+lang)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment