JSON Template can currently format JSON structures of *fixed depth*.
That is, the depth of the structure is known at template-writing-time.
This fits a lot of JSON structures, but parse trees can be represented
as JSON trees, and their depth is variable. So with a very small
amount of code, I've added *template recursion* to JSON Template.
This follows the templates-as-formatters concept described in one of
my articles: http://json-template.googlecode.com/svn/trunk/doc/On-Design-Minimalism.html
So there are no new concepts in the language, but there is some API
support for recursion.
So basically you can do
{subtree|template foo}
And the template 'foo' is used to format the var 'subtree'. Note that
in JSON Template variables can be JSON objects themselves -- they
don't have to be just strings and integers.
Templates can be mutually recursive and self-recursive by using {s|
template SELF} (SELF is a magic string, you don't need to give it a
name)
Here is an example of formatting parse trees I generated from
Narcissus:
http://github.com/andychu/narcissus/blob/master/codegen/sexp.py
With this feature, I also noticed that JSON Template becomes closer to
StringTemplate, which is used for the ANTLR parser generator.
So I think it's pretty nice that the language is flexible enough to
use for code generation now, without any language changes! It needs
to be polished a bit, and I probably will want auto-indenting for
generating code. But that will also be useful for generating web
content (HTML/XML/JS/CSS all have indentation).
Andy
That article described how people could implement "includes" on top of
the language, but I don't think many people actually did that. So
having a built-in formatter "template" will make things work out of
the box a bit better.
That helps with: http://code.google.com/p/json-template/issues/detail?id=24
Andy
On Dec 30, 11:33 pm, Andy <a...@chubot.org> wrote:
> This is a note about a new feature I put in the Python version. I
> need to flesh it out and document it, but any feedback is welcome.
>
> JSON Template can currently format JSON structures of *fixed depth*.
> That is, the depth of the structure is known at template-writing-time.
>
> This fits a lot of JSON structures, but parse trees can be represented
> as JSON trees, and their depth is variable. So with a very small
> amount of code, I've added *template recursion* to JSON Template.
>
> This follows the templates-as-formatters concept described in one of
> my articles:http://json-template.googlecode.com/svn/trunk/doc/On-Design-Minimalis...
Place holders would come in real handy right now, and I'm not sure if
you were for doing that or not.
At any rate, I'll be taking a fresh look at json-template's js and php
versions soon. In particular, I'm going to try and model a new PHP
version on the changes in the JS version from last fall.
-steve--
On Dec 30 2009, 11:33 pm, Andy <a...@chubot.org> wrote:
> This is a note about a new feature I put in the Python version. I
> need to flesh it out and document it, but any feedback is welcome.
>
> JSON Template can currently format JSON structures of *fixed depth*.
> That is, the depth of the structure is known at template-writing-time.
>
> This fits a lot of JSON structures, but parse trees can be represented
> as JSON trees, and their depth is variable. So with a very small
> amount of code, I've added *template recursion* to JSON Template.
>
> This follows the templates-as-formatters concept described in one of
> my articles:http://json-template.googlecode.com/svn/trunk/doc/On-Design-Minimalis...
I only touched the Python implementation I believe... I was pretty
satisfied with it.
> Place holders would come in real handy right now, and I'm not sure if
> you were for doing that or not.
What do you mean by placeholders?
> At any rate, I'll be taking a fresh look at json-template's js and php
> versions soon. In particular, I'm going to try and model a new PHP
> version on the changes in the JS version from last fall.
OK sounds good. Let me know if you have questions.
Andy