Discussion:
No documentation for predicates
Pankaj Garg
2011-03-25 16:22:43 UTC
Permalink
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.
--
You received this message because you are subscribed to the Google Groups "JSON Template" group.
To post to this group, send email to json-template-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to json-template+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit this group at http://groups.google.com/group/json-template?hl=en.
Andy Chu
2011-03-26 08:24:16 UTC
Permalink
Post by Pankaj Garg
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.
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
--
You received this message because you are subscribed to the Google Groups "JSON Template" group.
To post to this group, send email to json-template-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to json-template+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit this group at http://groups.google.com/group/json-template?hl=en.
Pankaj Garg
2011-03-26 18:06:56 UTC
Permalink
Thanks a lot. That was really helpful.
--
You received this message because you are subscribed to the Google Groups "JSON Template" group.
To post to this group, send email to json-template-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to json-template+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit this group at http://groups.google.com/group/json-template?hl=en.
Loading...