Here is some information about the technologies used in my application:
- Microsoft Visual Studio Enterprise 2022 (64-bit) - Current
- Stubble.Core version 1.10.8 ( library used to apply Mustache "logic-less templates" to json input data )
- .NET 6.0
Here is the snippet of the C# code that invokes Stubble API:
var objData = (JObject)JsonConvert.DeserializeObject(jsonData);
var stubble = new StubbleBuilder().Build();
string body = stubble.Render(template.TemplateBody, objData);
In our C# code, we have a string-based variable called template.TemplateBody which contains the following Mustache-based Stubble template:
{{#should_render}} If the relevant value in the custom_parameters_json
is true then this should be shown! {{/should_render}}
List seems to work as expected {{#my_list}} item: {{.}} {{/my_list}}
{{#truthy_value}} Truthy values should also work {{/truthy_value}}
{{#some_string}} String should be rendered if it is not null {{.}}
{{/some_string}}
Here is a generalized format of the JSON-based input test data:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"should_render": { "type": "boolean" },
"my_list": { "type": "array" },
"truthy_value": { "type": "integer" },
"some_string": { "type": "string" }
}
}
In our C# code, we also have a string-based variable called jsonData:
{
"should_render":true,
"my_list": ["a","b","c"],
"truthy_value":1,
"some_string":"My String"
}
Ultimately, jsonData will be deserialized into a C# object, stored in a variable called objData:
{
"should_render": true,
"my_list": ["a", "b", "c"],
"truthy_value": 1,
"some_string": "My String"
}
After applying the Stubble template to the JSON object data, it renders the following output:
\nList seems to work as expected\nitem: a\nitem: b\nitem: c\n\n\n
In the input JSON test data, should_render is set to true:
"should_render": true
Therefore, I am asking:
- Why did the Stubble API fail to render the statement "If the relevant value in the custom_parameters_json is true then this should be shown!" when
should_renderwas set to true?
{{#should_render}} If the relevant value in the custom_parameters_json is true then this should be shown! {{/should_render}}
- Why did the Stubble API fail to render the statement "Truthy values should also work" when
truthy_valuewas set to 1?
{{#truthy_value}} Truthy values should also work {{/truthy_value}}
- Why did the Stubble API fail to render the statement "String should be rendered if it is not null {{.}}" when
some_stringwas populated?
{{#some_string}} String should be rendered if it is not null {{.}} {{/some_string}}
Could someone please explain what modifications might be needed to get the following rendered output?
If the relevant value in the custom_parameters_json is true then this should be shown!
List seems to work as expected item: a item: b item: c
Truthy values should also work
String should be rendered if it is not null My String