No documentation for predicates

39 views
Skip to first unread message

Pankaj Garg

unread,
Mar 25, 2011, 12:22:43 PM3/25/11
to json-t...@googlegroups.com
I am new to json-templates.
As I was browsing through the documentation and group posts, I got to know about the predicate feature, but there is no clear documentation about it in the Wiki docs.

It will be very helpful for beginners to get around, if the documentation is updated.

Andy Chu

unread,
Mar 26, 2011, 4:24:16 AM3/26/11
to json-t...@googlegroups.com, Pankaj Garg

Yeah good point -- I actually just updated it for Python and
JavaScript a few days ago. I will update it and write something, but
for now the best examples are in the tests.

http://code.google.com/p/json-template/source/browse/jsontemplate_test.py
(search for PredicatesTest)

The gist is:

Predicates allow you to conditionally include a portion of the
template. They have access to the current value/node as well as the
"current context". Unlike "sections", they don't change the context
for variable lookup.

1. In terms of the API, predicates can be registered like formatters,
either through a simple dictionary or through the various *Registry
classes, like PrefixRegistry

2. In terms of the template syntax, it is simple {.if} {.or}

{.if singular}
One
{.or plural}
More than one
{.or}
None
{.end}

"singular" and "plural" are the names that are registered with the
Template when it's constructed.

There are a few built-in predicates. A very common one is "test",
which will test for an attribute:

{.if test debug}
Some debug text
{.end}

That is, if you have {"debug": true} in your JSON, this will be shown.

There is also a shortcut for "test" that lets you use ? --

{.debug?}
Some debug text
{.end}

That is identical to the above. Hope that helps.

Andy

Pankaj Garg

unread,
Mar 26, 2011, 2:06:56 PM3/26/11
to json-t...@googlegroups.com, Pankaj Garg, Andy Chu
Thanks a lot. That was really helpful.
Reply all
Reply to author
Forward
0 new messages