Veil icon indicating copy to clipboard operation
Veil copied to clipboard

Don't throw an exception when the handlebars template reference an not existing property

Open yannisgu opened this issue 7 years ago • 2 comments

At the moment when a handlbars template uses a not existing property, Veil will crash. So when I have the following expression:

{{myProp}}

But there is no {{myProp}} Veil will throw an Exception ("Unable to parse model expression", https://github.com/csainty/Veil/blob/master/src/Veil.Handlebars/HandlebarsExpressionParser.cs#L57). In our case it would be much better if this expression would be treated as null value. Why? We develop the handlbars templates and the according models are developed by different people. We want that properties can be added to the handlbars template, but added later to the model, without to break our mainline.

Therefor I propose to change this, so that when a member doesn't exist, Veil treats it as null-String.

In order to do this we would need to make the following additions to Veil:

  • https://github.com/csainty/Veil/blob/master/src/Veil.Handlebars/HandlebarsExpressionParser.cs#L57 Return an SyntaxTreeExpression.Empty() (new class) instead of throwing the exception
  • https://github.com/csainty/Veil/blob/master/src/Veil/Compiler/VeilTemplateCompiler.ParseExpression.cs#L20 Adding an if for our new EmptyExpressionNode In this case ParseExpression should return Expression.Constant(null, typeof(string))

What do you think?

yannisgu avatar Jun 14 '17 16:06 yannisgu

I can see the value in this, though I am not sure I would want that as standard behaviour. It will silently hide typos in the templates which will be hard to track down.

Made configurable though, I see no reason not to support it.

csainty avatar Jun 26 '17 07:06 csainty

Cool - what would be your favored way to configure this? Constructor ofVeilEngine?

yannisgu avatar Jun 26 '17 08:06 yannisgu