{.section inner} {foo} {.section outer} {@} {.or} Fail {.end}
{.end}
Using the following dictionary:
{ 'outer' : 'OUTER',
'inner' : { 'foo' : 'FOO', },
}
In this case, I'm trying to use .section as a conditional test. This
can be satisfied using the custom predicates you're talking about.
E.g.:
{.section inner} {foo} {.if outer?} {outer} {.or} Fail {.end}
{.end}
That's how I'm currently working around the problem. Though, as a
separate issue, neither FromString() and FromFile() take a
"more_predicates" parameter, which sort of breaks things. I think I
can double-workaround using the undocumented _constructor parameter,
but I haven't tried it yet.
However, I'm confused by the .section lookup logic. It seems like it
should be consistent with normal variable lookups. I think it can be
done by decoupling the name lookup traversal from the section stack.
For name lookup, think of the JSON structure as a tree and the current
context as a pointer to a node in the tree. A lookup starts at the
current context walks up the tree to the root until a match is found.
There would need to be a way to get each context's parent during the
lookup. This could be done either by injecting parent pointers into
the JSON tree or by keeping the path to the root on each context.
The section stack just keeps track of your current starting point for
lookups. Only the top element is directly used for any given lookup.
When you push a new section, you do a normal lookup from the current
context to find the new context, then push it on the stack.
Given the current limitations on sections, I believe the proposed name
lookup rule is the same for variables. What we get is generality on
sections. You can start a higher level section or even a nested
section (e.g {.section foo.bar}). It also handles the conditional
predicate usage.
-Josh
On Dec 12, 4:54 pm, Andy <a...@chubot.org> wrote:
> *** I thought I responded this morning -- the post isn't showing up in
> Groups, but luckily I saved a copy ***
>
> > At this point I'm thinking json-template is less elegant than a
> > standard templating language. Wouldn't it be much easier to have
> > access to the global namespace / root elements?
>
> > like
>
> > {.section $admin}
> > {.end}
>
> > but I'm having the feeling that I'm missing something. Could you
> > explain for what we need predicates?
>
> For the case of testing if "admin" is set at the root, it's possible
> that could work.
>
> But in general it doesn't work. There were some very long discussions
> about this on the group. This might get you to them since Groups
> search is also broken:http://www.google.com/webhp?hl=en#hl=en&source=hp&q=predicates+json+t...
This sounds a lot like Acquisition in Zope, which was one of the ideas
I really liked in Zope
(http://www.google.com/search?q=Zope+Acquisition). Could be very
powerful, but maybe it shouldn't be the default behaviour.
Sorry for the delay in responding.
Why is that a workaround? That is the intended usage of predicates.
(although I think that the if and ? are a bit redundant -- I may have
to tweak that)
> separate issue, neither FromString() and FromFile() take a
> "more_predicates" parameter, which sort of breaks things. I think I
> can double-workaround using the undocumented _constructor parameter,
> but I haven't tried it yet.
That is a bug -- those should have a more_predicates arg. Maybe I'll
also take this opportunity to rename it to "predicates" (but preserve
backward compatibility with an alias).
> However, I'm confused by the .section lookup logic. It seems like it
> should be consistent with normal variable lookups. I think it can be
> done by decoupling the name lookup traversal from the section stack.
I've considered this but think it's a bit confusing. Right now
sections only take you toward the "child" end of the tree. If you
could go "backwards", it seems like it would lead to some weird
semantics. So far it looks like predicates cover all these cases.
> For name lookup, think of the JSON structure as a tree and the current
> context as a pointer to a node in the tree. A lookup starts at the
> current context walks up the tree to the root until a match is found.
> There would need to be a way to get each context's parent during the
> lookup. This could be done either by injecting parent pointers into
> the JSON tree or by keeping the path to the root on each context.
That's a correct model of the JSON structure. But that is already
done for substitutions. It just maintains a stack of parents every
time you push the context. (See the _ScopedContext implemenetation -
PushSection/Pop, etc.)
>
> The section stack just keeps track of your current starting point for
> lookups. Only the top element is directly used for any given lookup.
> When you push a new section, you do a normal lookup from the current
> context to find the new context, then push it on the stack.
>
> Given the current limitations on sections, I believe the proposed name
> lookup rule is the same for variables. What we get is generality on
> sections. You can start a higher level section or even a nested
> section (e.g {.section foo.bar}). It also handles the conditional
> predicate usage.
Well the predicates are a little more general because they chain like
switch/case. And also they are user-customizable like formatters. We
went through some long discussions about putting predicates directly
in sections, but in the end it was too complicated. Having them as
separate concepts is the cleanest thing.
I'll file some bugs on your feedback.
thanks,
Andy