jehugaleahsa / mustache-sharp

An extension of the mustache text template engine for .NET.
The Unlicense
306 stars 78 forks source link

Exceptions when placeholder not found #1

Closed PaulGrimshaw closed 11 years ago

PaulGrimshaw commented 11 years ago

I would like an option to prevent an exception being thrown when a field is not found, simply replacing it with something like [Field "MyTag" not found!]. This would enable the template engine to carry on, and most of it would be rendered correctly.

Great library by the way, keep it up!

jehugaleahsa commented 11 years ago

That's a great idea. Expect a new release soon. On Apr 18, 2013 3:47 PM, "PaulGrimshaw" notifications@github.com wrote:

I would like an option to prevent an exception being thrown when a field is not found, simply replacing it with something like [Field "MyTag" not found!]. This would enable the template engine to carry on, and most of it would be rendered correctly.

Great library by the way, keep it up!

— Reply to this email directly or view it on GitHubhttps://github.com/jehugaleahsa/mustache-sharp/issues/1 .

PaulGrimshaw commented 11 years ago

I guess extending this slightly, it would be nice if you had the option to "Render Exceptions into the resulting document", for all exceptions, not just "field not found". That would allow users to see where they might have gone wrong.

However, as the users may often not be experienced programmers, a "friendly" error message such as [Field "MyTag" not found!] for all errors would be useful.

Thanks for the prompt response!

jehugaleahsa commented 11 years ago

I have published a new version to NuGet and uploaded the changes to GitHub.

After you call Compile and get a Generator back, you can add an event handler like this:

generator.KeyNotFound += (obj, args) =>
{
    args.Substitution = String.Format("[Field \"{0}\" not found!]",

args.MissingKey); args.Handled = true; // tells the code to not throw an exception };

Thanks for your feedback. It's good to know someone is looking at this stuff!

On Thu, Apr 18, 2013 at 7:02 PM, PaulGrimshaw notifications@github.comwrote:

I guess extending this slightly, it would be nice if you had the option to "Render Exceptions into the resulting document". That would allow users to see where they might have gone wrong.

However, as the users may often not be experienced programmers, a "friendly" error message such as [Field "MyTag" not found!] for all the errors would be useful.

Thanks for the prompt response!

— Reply to this email directly or view it on GitHubhttps://github.com/jehugaleahsa/mustache-sharp/issues/1#issuecomment-16618076 .

PaulGrimshaw commented 11 years ago

Fantastic, implemented already.

Slight correction in your code (Substitute, not Substitution):

generator.KeyNotFound += (obj, args) => { args.Substitute = String.Format("[Field \"{0}\" not found!]",args.MissingKey); args.Handled = true; };

Do you have somewhere where we can list our "desired features"? Or is here the best place?

PaulGrimshaw commented 11 years ago

Would it be possible to make the "KeyNotFOund" option to work with nested properties too?

e.g. {{customer.namee}} would return "[Field "customer.namee" not found!]", even if the customer object exists

jehugaleahsa commented 11 years ago

I probably should have passed the full name, rather than just the shortened name.

On Fri, Apr 19, 2013 at 2:41 AM, PaulGrimshaw notifications@github.comwrote:

Would it be possible to make the "KeyNotFOund" option to work with nested properties too?

e.g. {{customer.namee}} would return "[Field "Customer.Name" not found!]", even if the customer object exists

— Reply to this email directly or view it on GitHubhttps://github.com/jehugaleahsa/mustache-sharp/issues/1#issuecomment-16638243 .

jehugaleahsa commented 11 years ago

I changed the MissingKeyEventArgs class to now have a Key and MissingMember property. The MissingMember property holds the same value as it did before. Key contains the fully-qualified key that appears in the template.

jehugaleahsa commented 11 years ago

I also found a bug in the code where it wasn't making sure sub-members existed. In these cases, it was probably just throwing a KeyNotFoundException.

PaulGrimshaw commented 11 years ago

Great. The bug was the problem I was running into, so will update to get latest version. Many thanks, this library is pretty core to my current project!