Nim: how to get property of json in stdtmpl?

Created on 13 Mar 2018  路  5Comments  路  Source: nim-lang/Nim

I wrote the code.

#? stdtmpl | standard
#import json
#proc generateIndexHTMLPage(title: string, tabs: openarray[JsonNode]): string =
#  result = ""
<head><title>$title</title></head>
<body>
  <div id="menu">
    <ul>
  #for item in items(tabs):
    <li><a href=
    #item["id"]
    >
    #item["name"]
    </a></li>
  #end for
    </ul>
  </div>
</body>

item is json like this {"id":1, "name": "hoge"}.

item is type JsonNode.
So, I should get id like item["id"]

expression 'item["id"]' is of type 'JsonNode' and has to be discarded

error.

however, I changed discard item["id"], it never return string...

All 5 comments

I changed like this.
I used ${item["name"]}.
perhaps inside of ${} can use code?

#? stdtmpl | standard
#import json
#proc generateIndexHTMLPage(title: string, tabs: openarray[JsonNode]): string =
#  result = ""
<head><title>$title</title></head>
<body>
  <div id="menu">
    <ul>
  #for item in items(tabs):
    <li><a href=users/${item["id"]}>${item["name"]}</a></li>
  #end for
    </ul>
  </div>
</body>

screen shot 2018-03-13 at 18 50 36

However, double quote, " was shown...
And, multi byte string, like Japanese can't?

haha. I resolved myself!!

    <li><a href=users/${item["id"]}>${item["name"].str}</a></li>

Glad you got it to work :)

Can I close?

@andreaferretti yes!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

treeform picture treeform  路  28Comments

dom96 picture dom96  路  47Comments

Araq picture Araq  路  74Comments

zielmicha picture zielmicha  路  37Comments

kvinwang picture kvinwang  路  29Comments