views:

38

answers:

1

I have a big chunk of deeply-nested semi-structured JSON and would like to generate HTML from it. At the moment I'm using jQote2, but much of the code inside my templates deals with dynamically finding the next template to render and then calling it. What's a single <xsl:apply-tenplates> in XSLT takes several lines with JavaScript and jQuote. I dearly miss the pattern matching capabilities of XSLT. Is there any (templating) library in JavaScript that allows me to dynamically decide from the data which template to render?

Here is an example of what I want. Suppose I have a JSON structure like this:

{
  items:[
   {foo:1, bar:2},
   {foo:7, baz:99},
   {foo:8, quux:3}
  ],
  curdate:'2010-07-07'
}

I'd like to have a "root" template that renders the curdate field and then renders the items. If an item contains a "bar" field, I want the item to be rendered with a template named "tpl-bar" (or something like that), otherwise a template named "tpl-foo" should be used. Filtering capabilities (like "do not render items that have a quux field") would be a nice-to-have.

I am aware of the JSONT library, however from what I see it's not dynamic enough to accomplish what I described.

If no such library exists, I'm on the verge of giving it a shot myself. But I'm not sure how to do it at the moment. Code examples or general descriptions would help me.

+2  A: 

There's also JSLT, from what I remember reading it's a little more advanced than JSONT. I've never actually used it, though. The problem is that these libraries aren't hugely popular and so not a lot of work gets done to improve them and build upon them.

On the plus side, it's open source so if you don't find a feature you want you could attempt to add it yourself.

Andy E