Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

JavaScript client-side templating?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
xtf
Smarty n00b


Joined: 29 Oct 2004
Posts: 4

PostPosted: Fri Oct 29, 2004 12:13 pm    Post subject: JavaScript client-side templating? Reply with quote

Usually, templates are expanded server-side and then send to the client.
Would it be possible to just send the template parameters and the templates itself to the client and let the client expand them itself via JavaScript?
Large templates which are used often (for example, a table row) don't have to be send multiple times then.
The advantage is also that the response is usually a lot smaller, which also means less CPU usage on the server.
Back to top
View user's profile Send private message
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Fri Oct 29, 2004 12:39 pm    Post subject: Reply with quote

and it means, your site won`t work on a Browser which has JavaScripts disabled....
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri Oct 29, 2004 12:59 pm    Post subject: Reply with quote

@xtf: when you say "possible" I assume you mean, "does Smarty support that" and the answer is no. I must say, though, that it is an idea I've been turning over in my head for quite awhile. Of course, the compiling thing would have to go out the window, but who cares when it is on the client machine, yes? Of course, there is already a similar thing available for browsers that can render XML/XSLT.
Back to top
View user's profile Send private message
xtf
Smarty n00b


Joined: 29 Oct 2004
Posts: 4

PostPosted: Sat Oct 30, 2004 10:51 pm    Post subject: Reply with quote

kills wrote:
and it means, your site won`t work on a Browser which has JavaScripts disabled....

In that case, you can serve non-JS pages.
Back to top
View user's profile Send private message
xtf
Smarty n00b


Joined: 29 Oct 2004
Posts: 4

PostPosted: Sat Oct 30, 2004 10:53 pm    Post subject: Reply with quote

boots wrote:
@xtf: when you say "possible" I assume you mean, "does Smarty support that" and the answer is no. I must say, though, that it is an idea I've been turning over in my head for quite awhile. Of course, the compiling thing would have to go out the window, but who cares when it is on the client machine, yes? Of course, there is already a similar thing available for browsers that can render XML/XSLT.

You'd still need to compile the normal templates into JS code.
And (manually) using XSLT means your site requires it, while adding JS support to the template engine means you can serve both JS and non-JS (and maybe XSLT) pages without modification to your templates.
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun Oct 31, 2004 12:21 am    Post subject: Reply with quote

...but ideally you wouldn't want to do the compilation on the server side, right? Otherwise, why bother at all? The issue I see with compiling on the client side is finding a way to store/retrieve the compilations. Besides, would it matter? Compiling is important at the server since you are trying to serve many clients at once. If you have single dedicated client (running on a modern machine/browser) performance probably isn't the number one issue.

Maybe that's not what you are getting at though? Like I said, over the years I've had several ideas on this depending on where the tech was at the time. Long ago I wrote some primitive templating classes for JS but most recently my thought goes like this: smarty template plugins to compile PHP data structures into JS equivalents and additional smarty plugins that delayed things like loop processing until the client side (ie: given data passed to a loop, capture the contents of the loop into a js var, compile the data into a JS array and send the resulting structures down to the browser for the actual loop processing by a local JS function). The thing is, while all this uber-meta-programming sounds like fun, I'm not sure how much something like that would really help.

If you have some ideas on how it might work, I'm all ears.

BTW: I only mentioned XML/XSLT for comparison and to lend some credence to the idea--others have already come up with solutions, even if I don't think much of them (I'm not a big XSLT fan to say the least).
Back to top
View user's profile Send private message
xtf
Smarty n00b


Joined: 29 Oct 2004
Posts: 4

PostPosted: Sun Oct 31, 2004 12:19 pm    Post subject: Reply with quote

With compiling I meant transforming the source templates into JS code. I didn't mean template instantiation.
You wouldn't want to generate the JS code again and again.
Quote:
and additional smarty plugins that delayed things like loop processing until the client side (ie: given data passed to a loop, capture the contents of the loop into a js var, compile the data into a JS array and send the resulting structures down to the browser for the actual loop processing by a local JS function).

That's one option.
You might be able to do this without changing lots of code.
The main idea is to replace 'lots of HTML and some data' with 'one JS function call and some data'.
The first step is to generate a single JS file with all JS code. Transform any sequence of HTML code and data without logic in the input templates into a JS function. Then, instead of putting the HTML and data into your compiled templates, put the JS function call with data into it.

After that's done, you could look into transferring logic from the server to the client.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP