On Fri, Aug 21, 2009 at 4:45 AM, Jason Davies<[email protected]> wrote:
>
> I'm interested in compiling the templates server-side, so that they
> can be rendered very quickly on the client-side. I noticed this is a
> TODO item in the JavaScript implementation. Is there anything else to
> consider aside from simply splitting _Compile into a separate module?
>
> What form should the compiled template take?
_Compile currently compiles to tree of objects in memory (which are
then "interpreted") In contrast compiling on the server side would
mean compiling directly to JavaScript code.
{.section} -> if statement
{.repeated section} -> for loop
substitution -> call print callback with formatted value
It's possible to compile to JSON or something, but given that JSON
isn't even native in most browsers, it probably will be a lot slower.
You would save sending the parser over the wire, but I'm not sure how
much of a win that is.
Are you sure you need the speed? A performance test would answer
these questions. That can be set up with the existing test
infrastructure (tests in either V8, the builtin Windows JS Engine, or
generates HTML for a browser test)
The tests are a little awkward now because they have dependencies in a
different svn repo. If you need help let me know.
> If possible I would like the client-side part to be as short and fast
> as possible. I would even consider going to the extreme and making it
> so the compilation step generates something like this to send to the
> client:
>
> (function(d){function c(x){callback(c);}c('<html><body>');c(formatter
> (d.foo.bar));c('</body></html>');})({foo:{bar:'baz'}});
>
> This would then be very quick to eval on the client-side.
Right, that is the idea, but I would assume you don't compile the data
in. Otherwise you would just send the expanded string over. The
compiled template can be applied many times to different JSON retrieve
say via AJAX. You want to compile to a function with this signature
(roughly):
function compiledTemplate(data, callback, formatters) {
f1 = formatters.f1;
f2 = formatters.f2;
c("<hello there>");
c(f1(data.foo));
for (int i=0; i<data.bar.length; i++) {
c(f2(data.bar[i])
}
return expandedString;
}
document.write(compiledTemplate(data1));
document.write(compiledTemplate(data2));
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
-~----------~----~----~----~------~----~------~--~---