fenekku / moustachu

Mustache templating for Nim
65 stars 8 forks source link

possible feature: support for assoc array param in newContext #10

Closed JohnAD closed 5 years ago

JohnAD commented 6 years ago

This is one of two features I'd be interested in adding to this library.

Starting with the following example:

import moustachu

var t = """
<html>
  <body>
    <h1>{{forum_title}}</h1>
    <ul>
    {{# messages}}
      <li>content: {{content}}, author: {{author}}</li>
    {{/ messages}}
    </ul>
  </body>
</html>
"""

var messages: seq[Context] = @[]
var m = newContext()
m["content"] = "blah1"
m["author"] = "Joe"
messages.add(m)

var c = newContext()
c["forum_title"] = "XYZ"
c["messages"] = messages
echo(render(t, c))

It would be nice newContext had another variant that accepted an array of tuples, such that you could do:

import moustachu

var t = """
<html>
  <body>
    <h1>{{forum_title}}</h1>
    <ul>
    {{# messages}}
      <li>content: {{content}}, author: {{author}}</li>
    {{/ messages}}
    </ul>
  </body>
</html>
"""

var messages: seq[Context] = @[]
messages.add(newContext({"content": "blah1", "author": "Joe"}))  # this is the change

var c = newContext()
c["forum_title"] = "XYZ"
c["messages"] = messages
echo(render(t, c))

If interested in this feature, I'll start a PR this week. We will want to add border case tests.

JohnAD commented 6 years ago

Additionally, I would add this to the documentation.

In fact, I recommend adding the much of the generic 'moustache' documentation to this documentation. That way new users are less likely to get lost. I'd be happy to do that as well.

fenekku commented 5 years ago

12 merged. I am closing this.