Template troubles resolved.

So I found exactly what I was looking for. It’s called json-template, and it is soooooooooo perfect.

You create a template …

<li id="edge_{id}">
  <a href="/{source}" class="source">{source}</a> is a 
  <a href="/{target}" class="target">{target}</a> 
  <a href="#edge_delete-{id}" class="edge_delete">&nbsp;</a>
</li>

… You slap it together with a PHP (or Java or Python) object on the server …

<?php
$t_file = file_get_contents('node_li.jsont.php');
$t = new JsonTemplate($t_file);
$d = json_encode($model);
 
echo $t->expand($d);
?>

… You slap it together with a JSON object in the browser …

$.post('/edges.json', form.serialize(), function(data, textstatus){
  if(textstatus=="success"){
    $.jsont('node_li', data.edge).insertAfter("ul.edges li:last");
  }
}, "json");

… and you get the same HTML.

<li id="edge_80">
  <a href="/test" class="source">test</a> is a 
  <a href="/asdf31113" class="target">asdf31113</a> 
  <a href="#edge_delete-80" class="edge_delete">&nbsp;</a>
</li>

I just finished wrapping up the Javascript class as a jQuery plugin ($.jsont), and next I’m going to wrap the PHP class in a view helper and she’ll be set to jet.

Simple.

1 Comment so far

  1. Kris Jordan on August 27th, 2009

    This is sweet Kev – great find. Hope to check out the github bits soon.

Leave a reply