I have run into this issue of needing a "template global variable" in a formatter before, say for a base URL. Now you're asking for this in predicates. I looked it up and Django has this scheme:
http://stackoverflow.com/questions/1030249/defining-global-variable-in-django-templates Perhaps the template constructor or expand() function can have some sort of object to be available to both formatters and predicates. I'm thinking of something like the "stacking namespaces" approach which seems to be mentioned in the RequestContext docs. The goal would be to write your thing as: {.section post} Title: {title} Body: {body} {section @ is-moderator?} <-- The problem here is that the predicate is passed "post" and has no other context <button id="{id}"> {.end} {.end} Although I don't think it's the end of the world of "post" needs to have a key to say whether to show the button. Andy On Mon, Sep 21, 2009 at 10:37 PM, Andy Chu <[email protected]> wrote: > On Mon, Sep 21, 2009 at 11:58 AM, Steven Roussey <[email protected]> wrote: >> >> >>> I'm not sure what you're getting at here. Can you post a real >>> example? "foo" and "advancedSetting" are BOTH keys in your JSON >>> dictionary. >> >> Yes, exactly. >> >>> The predicate is a function which receives a JSON node. The JSON node >>> is named in the section. >> >> Yes, but... >> >> data={ moderator:true, post: {id:52, title:"Hi", >> message:"there"},... } >> >> {.section post moderator|true?} >> <button id="{id}">Delete</button> >> {.end} >> >> The way I implemented it, if the text "moderator|" was omitted, the >> meaning would simply be a test on post|true? Having both json nodes >> means that I can step into the post node while testing on the >> moderator node. > > OK, so you want to only display a delete button if the current user is > a moderator. That's an interesting case because the moderator is > global and not part of the post. > > I guess the short answer now is that you have to craft your JSON for > the template, and you would to put {"show-delete-button": true, > "title": ...} inside every post. > > So the logic goes in the app instead of the template. The philosophy > is to put "business logic" in the app and the template only deals with > display. I would be interested to see how you would do this in other > template languages. > > But I think there are too many degrees of freedom in what you propose. > You aren't using the predicate part in your example. Another option > is to have the predicate have access to the entire dictionary, rather > than just the current node. Then the predicate could see "moderator". > It is sort of a waste of space to put the same truth value in every > node. I'll have to think about it. > > >> Also, I implemented parameters (to formatters and predicates). >> >> {.section post role|isValue("moderator")?} >> <button id="{id}">Delete</button> >> {.or post role|isValue("writer")?} >> <button id="{id}">Reply</button> >> {.end} >> >> {.section post|cleanlanguage("G") } >> {.message} >> {.end} >> >> or >> >> {.section post } >> {.message|cleanlanguage("G")} >> {.end} >> >> The parameters are appended. I'm not sure why you omitted parameters. >> (I only allow parameters that can be encoded as json). I can see now >> why you omitted expressions, and changed my version to strip that out >> and use predicate functions. But I don't see why there are no >> parameters. Yet. > > So this is already possible. See formatters.py for a Python example. > It will be possible in JS too, because the more_formatters arg is a > function *which returns functions*. So you need to use closures in > JS. This is the most flexible scheme, and doesn't confine users to > one kind of parameter passing syntax. You can do what you did solely > with more_formatters and without touching the source. > > The most prominent example is the 'template-file' formatter: > {node|template-file another_file.jsont} > > TemplateFileInclude in formatters.py is a callable that returns > another function (a bound method) > > thanks, > Andy > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "JSON Template" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/json-template?hl=en -~----------~----~----~----~------~----~------~--~---
