On Mon, May 25, 2009 at 5:55 AM, Martijn Faassen <[email protected]> wrote:
>
> Hi there,
>
> NOTE: if you see references to key or index generation below, or path
> generation, or reconstructing the json context based on javascript
> note, be aware that's NOT what I've implemented! Please do not let
> those ideas distract you to the point where can't review the actual
> work I did. :)
>
> On a branch here:
>
> https://json-template.googlecode.com/svn/branches/faassen-hooks
>
> I've implemented hooks for json-template (javascript). The code is
> prototype only (it may work, but it has no automated tests, as I still
> haven't figured out how to do this properly; it needs the ability to
> pass in specific javascript functions into the options dictionary).
>
> Here is the code for review:
>
> http://codereview.appspot.com/67105/show
>
> (this ignores a small patch I've made to pass 'options' to
> ScopedContext instead of options.undefined_str, that's independent)
>
> The idea is that during various phases of the expansion process, hook
> functions are called. I've defined 5 of them:
>
> * transformData: used to automatically transform incoming json data,
> for instance by amending it with path information
>
> * beforeSection, afterSection: called before and after a section are
> entered and left. Can be used to log, or to insert content into the
> output stream (call write).
>
> * beforeRepeatedSection, afterRepeatedSection: called before and after
> a repeat is taking place in a repeated section
>
> By default, an empty set of hooks is passed in (emptyHooks); these do nothing.
>
> Optionally a LoggingHooks can be passed in; this will write
> information to a supplied log object. With more hooks, the entire
> logging system could be made to work like this (and it would be easy
> to disable logging, or log to something else, etc).
>
> I've also supplied a MultiHooks; this demonstrates that you can pass
> in multiple hooks at the same time, for instance you may want to do
> logging and also do something else: Template({hooks:
> MultiHooks([LoggingHooks(log), jsonIdHooks])})
>
> What the hooks can generally be used for is:
>
> * logging

So I think for logging, it should be configured on a module-level
basis, not a Template class level basis.  That is, I should do:

jsontemplate.log = MyLogFunction

Not pass it in to every template.

The logging isn't really an important thing -- the only reason it's
there is because I'm not as good at inspecting JS code as I am at
Python.  I should probably just delete it all since the code is
debugged.  Notice that the Python implementation (and other
implementations) have no logging.

> * inserting extra data into the output for debugging (for instance
> "<!-- section foo -->" into HTML output).
>
> * insert ids into the HTML output automatically so that the place in a
> JSON structure can be reconstructed. (for instance <div
> style="display: none" class="json-id" id="foo.bar[3].baz" />) (not
> generate these ids, this can be done using the transformation below).

So, I think this should be done by modifying the data dictionary.  You
seem to have another requirement that the template string not be
modified.  In the absence of a real example, this just seems like too
much magic to me.

In particular, it seems to constrain the template so that the HTML DOM
tree matches the JSON tree structure.  They're both trees, but in
general, I don't see why they would need to match up.  It seems like
you'll have more DOM nodes than JSON nodes in most cases.

> * transforming data before it's passed in; this can be used to amend
> the json with key and index information, for instance.

This should be done by subclassing Template and making variants of the
expand/render functions, e.g. t.expand_with_indices(data_dict).
There's already a hook there, so we don't need another one (modulo the
JS-specific bug).

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
-~----------~----~----~----~------~----~------~--~---

Reply via email to