简体   繁体   中英

How to make Mustache logic tags functional

I'm currently using C# to create/parse a JSON file and then using this to fill in a mustache template.

I've installed Nustache into VB using the NuGet package for Nustache.

My JSON file looks like this:

{
  "LinkName": "test",
  "TESTBOOL": true,
  "NodeProfileENUM": "TEST",
  "Requester": false,
  "Responder": false,
  "Nodes": {
    "Root": {
      "nodeName": "Root",
      "displayName": null,
      "type": null,
      "initialValue": null,
      "serializable": null,
      "className": null,
      "enumName": null,
      "isLast": true,
      "actionList": {}
    }
  },
  "LinkActions": {}
}

And in my template I've tried using Tags like:

{{#TESTBOOl}} test {{/TESTBOOL}}

but I keep getting an error saying the block isnt registered,

I've also tried iterating through the Nodes section:

{{#each Nodes}}
{{#each this}}
{{this.nodeName}}
{{/each}}
{{/each}}

but this isn't working either.

I'm parsing and rendering the json and template (C#) like this:

string nodeProfileFormat = File.ReadAllText("NodeProfileTemplate2.txt");

JObject parsedLinkTest = 
JObject.Parse(File.ReadAllText("LinkDefinitionTest.json"));

string nodeProfileResultTest = 
Nustache.Core.Render.StringToString(nodeProfileFormat, parsedLinkTest);

File.WriteAllText("NodeProfileTemplateResult.java", nodeProfileResultTest);

Is there anything I'm forgetting to do?

To test your templates, use moustache's demo . Your TESTBOOL template is working if you fix the typo:

{{#TESTBOOL}} test {{/TESTBOOL}}

If you want to display nodeName you have to do:

{{Nodes.Root.nodeName}}

Your json contains only object. Your Nodes is not an array. Maybe you are looking to iterate properties though.

About your C#, you can use Nustache FileToFile method:

var parsedLinkTest = JObject.Parse(File.ReadAllText("LinkDefinitionTest.json"));
Nustache.Core.Render.FileToFile("NodeProfileTemplate2.txt", parsedLinkTest, "NodeProfileTemplateResult.java");

If it failed, check with the debugger if parsedLinkTest is well filled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM