StubbleOrg / Stubble

Trimmed down {{mustache}} templates in .NET
Other
401 stars 58 forks source link

[Question]: Configure the behavior if key is not found from model. #74

Closed Incarnation-p-lee closed 4 years ago

Incarnation-p-lee commented 4 years ago

Is there any way to configure the behavior of stubble, if the key of template is not found from provide map ?

For example, there is one template {{name}}, with Dictionary {"city": "abc"}, when try to Process by default the {{name}} will be replace to "" silently. Here I would like to execption out or complain the key is missing from Dictionary.

Romanx commented 4 years ago

Hi there,

Thanks for using Stubble. There is a setting on the RenderSettings argument that you can do this with. If you set ThrowOnDataMiss to true then it will throw when data is missing as you would like.

This setting gets passed into the render method. We should probably make this setting clearer so thank you for bringing it to our attention.

Thanks,

Incarnation-p-lee commented 4 years ago

@Romanx Thanks a lot, also locate it from code, suggest to add it to README.md.

snipervld commented 4 years ago

@Romanx Sorry for necroposting.

Maybe, it's possible to add a way to handle non-existing keys? This can be used to render custom text instead of missing value, e.g. "some text {{name}}" => "some text Key Is Missing", or even "some text {{name}}" => "some text {{name}}" (preserve key)

I've implemented whis behaviour myself in my project, but it would be nice, if there is a generic solution for this problem.