Issue 31: Implement predicates for selecting sections
Status:  Started
Owner: ----
Project Member Reported by sroussey, Sep 14, 2009
How to do an if statement? 

{.section foo=='bar'} 

some stuff {a:html}
 other junk {b:html} 
{.end}

{.section foo=='barbara'} 
more stuff {c:html} 
complete junk {v:html} 
{.end} 

? 

I sorta hacked it in, but obviously it is not in the other versions (only 
JS). But some sort of IF statement is essential, I think.
Sep 14, 2009
Project Member #1 gtempacc...@yahoo.com
Answered on the group, see discussions about "predicates":

http://groups.google.com/group/json-template/browse_thread/thread/f354e3c2108353b7#

http://groups.google.com/group/json-template/browse_thread/thread/1ef3327f6f9b5436#

The reason it's done this way is that I don't want to add any operators to the
language.  Once you add ==, then you need !=, >, >=, list membership, dictionary
membership, etc.  So the solution is to just define predicates in the host language
and refer to them in the template.

Comments welcome.  I haven't gone around to implementing it yet.

Sep 16, 2009
Project Member #2 sroussey
OK, not adding operators to the template language makes sense.

So 

{.section foo} 
stuff
{.end} 

is (will be) shorthand for:

{.section foo foo?} 
stuff
{.end}

What do you mean by filters?
And what are your ideas on how to implement?
Sep 16, 2009
Project Member #3 gtempacc...@yahoo.com
No it will be shorthand for

{.section foo true?}
stuff
{.end}

The default predicate denoted "true?" is like "bool()" in Python -- take any object
and convert it to True/False.

When I said "filters" there I meant "formatters", which come after the |.  Formatters
transform an arbitary value to a string.  Predicates convert an arbitrary value to a
boolean.

I don't think it should be too hard to implement.  It's just adding the predicate to
the parsing, looking it up at runtime, and deciding which section to expand.
Sep 16, 2009
Project Member #4 gtempacc...@yahoo.com
(No comment was entered for this change.)
Summary: Implement predicates for selecting sections
Sep 16, 2009
Project Member #5 sroussey
To me this mean that it will always run:

{.section foo true?}
stuff
{.end}

But a section won't show if there is no foo, correct?

To me, this seems like a better translation:

{.section foo}
stuff
{.end}

is the same as 

{.section foo foo|true?}
stuff
{.end}

depending on the name of the function. Could be isTrue

{.section foo foo|isTrue?}
stuff
{.end}

So we can do this (where stuff in [] denotes what is optional):

{.section scope[|formatter][ test[|predicate]?]}
stuff
{.end}

Does that look right?
Sep 17, 2009
Project Member #6 gtempacc...@yahoo.com
There are only 2 concepts: formatters and predicates.  You have 3 things there --
what is "test"?

I haven't committed to doing formatters on sections -- only predicates.  But if we
were to add it, then the syntax would be:

{.section foo plural?|html}
<b>hello</b>
{.or}
<i>bye</i>
{.end}

That formats the section as HTML no matter what -- showing the first clause if foo is
plural, otherwise showing the second clause.

There was a long discussion about the merits of combining these and the syntax on the
groups thread I pointed you to.  In sum, the formatters on sections seem nice, but
they complicate some aspects of implementation.  Maybe at some point though.


Sep 17, 2009
Project Member #7 sroussey
OK, I see. So no formatter, just predicates. But I'd like to see them optionally
based on different parts of the JSON.

JSON={
 advancedSetting:true,
 foo: ...
 bar: ...
 ...
}

{.section foo advancedSetting|true?}
<b>hello</b>
{.or}
<i>bye</i>
{.end}

{.section jj}
stuff
{.end}

{.section foo advancedSetting|true?}
<b>hello</b>
{.or}
<i>bye</i>
{.end}


Thus why I thought:
{.section foo}
was shorthand for:
{.section foo foo|true?}


I need something more flexible like this, so I'm likely going to have to fork. :(

But any other good changes I make I'll port over. I was hoping not to get into the
implementation so much, but I guess that will give me the opportunity to have better
compilation (like the php version creating compiled php files like smarty).
Sep 17, 2009
Project Member #8 sroussey
I meant:

{.section foo advancedSetting|true?}
<b>hello</b>
{.or}
<i>bye</i>
{.end}

{.section jj}
stuff
{.end}

{.section bar advancedSetting|true?}
<b>hello</b>
{.or}
<i>bye</i>
{.end} 

the second section having bar not foo.... 

I'll move the discussion over to the Google Group from here on out. Sorry to clutter
up the issues list...
Nov 8, 2009
Project Member #10 andyc...@gmail.com
(No comment was entered for this change.)
Labels: Todo-Java Todo-PHP