- 1.4k
- 13
- 20
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 varvalue objDatain =the (JObject)JsonConvert.DeserializeObject(jsonData);custom_parameters_json
is true then this should be shown! {{/should_render}}
List seems to work varas stubbleexpected ={{#my_list}} newitem: StubbleBuilder(){{.Build();
}} {{/my_list}}
{{#truthy_value}} Truthy values should also work {{/truthy_value}}
{{#some_string}} String should be rendered if it stringis bodynot =null stubble.Render(template{{.TemplateBody, objData);}}
{{/some_string}}
In our C# code, we have a C# string-based variable called template.TemplateBody which contains the following mustache-based formatted 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 jsonJSON-based input testtest 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" } } }
{
"$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 C# string-based variable called jsonDatajsonData:
"{"should_render":true,"my_list":["a","b","c"],"truthy_value":1,"some_string":"My String"}"
{
"should_render":true,
"my_list": ["a","b","c"],
"truthy_value":1,
"some_string":"My String"
}
Ultimately, the jsonDatajsonData will be deserialized into a C# object-based, and placedstored in a variable called objDataobjData:
{{ "should_render": true, "my_list": [ "a", "b", "c" ], "truthy_value": 1, "some_string": "My String" }}
{
"should_render": true,
"my_list": ["a", "b", "c"],
"truthy_value": 1,
"some_string": "My String"
}
After, one applies applying the aforementioned stubbleStubble template to said jsonthe JSON object data, it renders the following output:
\nList seems to work as expected\nitem: a\nitem: b\nitem: c\n\n\n"
\nList seems to work as expected\nitem: a\nitem: b\nitem: c\n\n\n
In the input json-based input testJSON test data, the should_rendershould_render is set to true:
"should_render": true
"should_render": true
Therefore, I am asking:
- whyWhy did the Stubble API fail to render thatthe statement "If the relevant value in the custom_parameters_json is true then this should be shown!" when the should_render's value
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}}
{{#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 the truthy_value's value
truthy_valuewas set to 1?
{{#truthy_value}} Truthy values should also work {{/truthy_value}}
{{#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 the some_string's value
some_stringwas populated:?
{{#some_string}} String should be rendered if it is not null {{.}} {{/some_string}}
{{#some_string}} String should be rendered if it is not null {{.}} {{/some_string}}
Could someone please tell meexplain what modifications that one might need to make in orderbe needed to get 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
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
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 C# string-based variable called template.TemplateBody which contains the following mustache-based formatted 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 have a C# string-based variable called jsonData
"{"should_render":true,"my_list":["a","b","c"],"truthy_value":1,"some_string":"My String"}"
Ultimately, the jsonData will be deserialized into C# object-based, and placed in a variable called objData:
{{ "should_render": true, "my_list": [ "a", "b", "c" ], "truthy_value": 1, "some_string": "My String" }}
After, one applies the aforementioned stubble template to said 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-based input test data, the should_render is set to true
"should_render": true
Therefore,
- why did the Stubble API fail to render that statement "If the relevant value in the custom_parameters_json is true then this should be shown!" when the should_render's value was 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 the truthy_value's value was 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 the some_string's value was populated:
{{#some_string}} String should be rendered if it is not null {{.}} {{/some_string}}
Could someone please tell me what modifications that one might need to make in order 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
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
C# Stubble API for processing json-based data with a mustache-based logic-less template fails to render output data properly
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 C# string-based variable called template.TemplateBody which contains the following mustache-based formatted 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 have a C# string-based variable called jsonData
"{"should_render":true,"my_list":["a","b","c"],"truthy_value":1,"some_string":"My String"}"
Ultimately, the jsonData will be deserialized into C# object-based, and placed in a variable called objData:
{{ "should_render": true, "my_list": [ "a", "b", "c" ], "truthy_value": 1, "some_string": "My String" }}
After, one applies the aforementioned stubble template to said 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-based input test data, the should_render is set to true
"should_render": true
Therefore,
- why did the Stubble API fail to render that statement "If the relevant value in the custom_parameters_json is true then this should be shown!" when the should_render's value was 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 the truthy_value's value was 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 the some_string's value was populated:
{{#some_string}} String should be rendered if it is not null {{.}} {{/some_string}}
Could someone please tell me what modifications that one might need to make in order 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