StubbleOrg / Stubble

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

Mustache rendering into Json #105

Closed Koudysmen closed 4 years ago

Koudysmen commented 4 years ago

I am trying to render mustache into JSON file. There are few characters that have to be “json” escaped in order to get valid json file after rendering. I was thinking about options how to achieve this. First option that came to my mind was to replace default InterpolationTokenRenderer with overrriden customized renderer that would escape output string using Newtonsoft.Json library. However to do this I would need to replace this default renderer which is added in the RendererSettingsBuilder using RendereSettingsDefaults.DefaultTokenRenderes(). For this I need to create own StubbleBuilder, RendererSettingsBuilder and add all default renderers but the InterpolationTokenRenderer.

Another option which is provided within this pull request is to add EscapeForJson property into RenderSettings and make use of it within InterpolationTokenRenderer. By default this is set to false. If set to true, output string value from InterpolationTokenRenderer is replaced with escaped json string.

Added necessary dependencies: Newtonsoft.Json Version="12.0.3

Please let me know in case there is better option how to achieve this without copying too much code. I believe rendering into JSON is quite popular these days and this built-in functionality would be appreciated by many.

Example of template:

{
  “MyProp” : “{{{MyValue}}}”
}

Example of input value read via Json.Net extension:

{
  “MyValue” : “String with invalid \” json character”
}

Example of output with changes included in the pullrequest (valid json):

{
  “MyProp” : “String with invalid \” json character”
}

Example of output without changes included in the pullrequest (invalid json):

{
  “MyProp” : “String with invalid ” json character”
}

More examples are in test cases.

Romanx commented 4 years ago

Hi there,

Unfortunately this isn't something that I would accept into Stubble core since it's against our core aim of being compliant with standard mustache without any other frills.

You could package this up into an extension although I think that the neatest way to accomplish your goal would be to override the encoding function that we use although this would do so for all interpolation tags (that have {{{ anyways). This can be done through the RendererSettings.EncodingFuction which can do what you're already doing.

Hopefully this makes some sense, if not please let me know.

petermicuch commented 4 years ago

@Romanx, thanks for proposed solution for this PR. Is there a way to apply encoding function to helpers extensions as well? Since this is global rendering settings, I think this would make sense. I will open issue for helpers extension.

Romanx commented 4 years ago

Hi, your helpers would have access to the encoding function by accessing the context in your helper however there isn't an automatic way to have this applied. Please feel free to open an issue in helpers if you have any problems.

Thanks for using Stubble!

petermicuch commented 4 years ago

Ok, I will try to open issue or create PR for this on helpers side. In a use cases I discussed with several colleagues for different components that we have, this always made sense to be applied globally. If we want to skip encoding, then we have {{{}}} option. Although I am not sure if helpers work in that case, have to check.