jdiamond/Nustache — pull request #58 by mdevilliers (2013-08-09 13:44:24+00:00)
Title: Render context behaviour

Hey Jason,

Great project.

I was looking at the mustache documentation and came across a feature offered in the ruby implementation - namely to set an option to throw an exception of a missing data item during render.

For example rendering

"{{foo.one}} {{foo.two}}"

with the option enabled against the data

```
var dict = new Dictionary();
dict.Add("one" , "Hello");
```

would fail explicitly rather than rendering "Hello " <--- it has dropped the {{foo.two}} placeholder.

I initially looked at the compile template awesomeness which looked like a great way to achieve something similar however it seems to require a concrete definition to compile against (which makes sense). In our system we don't have a statically defined representation of our data e.g. it's made out of Dictionaries, DataTables, dynamic proxies etc. so we are unable to use this functionalilty as easily as we would like it. We were also wanting to specify other behavior e.g. guard against strings being empty.

However taking inspiration from the ruby implementation what I have come up with is something that looks like this -

```
var dict = new Dictionary();
dict.Add("one" , "Hello");
dict.Add("two" , true);

var behavior = new RenderContextBehaviour() {
RaiseExceptionOnDataContextMiss = true,
RaiseExceptionOnEmptyStringValue = true };

var ok = Render.StringToString("{{foo.one}} {{foo.two}}", new{ foo = dict }, behavior );

//throws NustacheContextMissException
var fails = Render.StringToString("{{foo.DOES_NOT_EXIST}} {{foo.two}}", new{ foo = dict }, behavior );

//change the dictionary value
dict["one"] = string.Empty ;

//throws NustacheEmptyStringException
var failsAsWell = Render.StringToString("{{foo.one}} {{foo.two}}", new{ foo = dict }, behavior );

```
It's not as pretty as Ruby :-)

Obviously all of the existing Render methods work as before with out the overload.

The RenderContextBehaviour object has the following properties (but could be extended beyond quite easily with other oprions)

RaiseExceptionOnDataContextMiss will throw when an item is null
RaiseExceptionOnEmptyStringValue will throw when a string is not null but is empty

I have added some unit tests with examples and kept the original api intact.

It would be great if you could have a chance to feedback on the changes.

Thanks,

Mark

PS I didn't make any changes to ValueGetterFactory but the Git gods think I did!!