codegooglecom / json-template

Automatically exported from code.google.com/p/json-template
0 stars 0 forks source link

Implement predicates for selecting sections #31

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
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.

Original issue reported on code.google.com by sroussey on 15 Sep 2009 at 4:55

GoogleCodeExporter commented 9 years ago
Answered on the group, see discussions about "predicates":

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

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

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.

Original comment by gtempacc...@yahoo.com on 15 Sep 2009 at 6:54

GoogleCodeExporter commented 9 years ago
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?

Original comment by sroussey on 16 Sep 2009 at 11:04

GoogleCodeExporter commented 9 years ago
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.

Original comment by gtempacc...@yahoo.com on 17 Sep 2009 at 4:06

GoogleCodeExporter commented 9 years ago

Original comment by gtempacc...@yahoo.com on 17 Sep 2009 at 4:07

GoogleCodeExporter commented 9 years ago
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?

Original comment by sroussey on 17 Sep 2009 at 4:51

GoogleCodeExporter commented 9 years ago
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}
hello
{.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.

Original comment by gtempacc...@yahoo.com on 17 Sep 2009 at 3:48

GoogleCodeExporter commented 9 years ago
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?}
hello
{.or}
<i>bye</i>
{.end}

{.section jj}
stuff
{.end}

{.section foo advancedSetting|true?}
hello
{.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).

Original comment by sroussey on 17 Sep 2009 at 10:04

GoogleCodeExporter commented 9 years ago
I meant:

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

{.section jj}
stuff
{.end}

{.section bar advancedSetting|true?}
hello
{.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...

Original comment by sroussey on 17 Sep 2009 at 10:07

GoogleCodeExporter commented 9 years ago
Started in Python:
http://code.google.com/p/json-template/source/detail?r=1fd56e7734c6e5f7455526402
ee71002291068b2

Original comment by gtempacc...@yahoo.com on 30 Sep 2009 at 6:01

GoogleCodeExporter commented 9 years ago

Original comment by andyc...@gmail.com on 9 Nov 2009 at 6:58