Ankit
Ankit

Reputation: 153

Can smarty template engine be used to create templates on client side?

My web application uses Smarty templating system on server side to generate content. My application would be using a lot of AJAX interaction. Instead of generating the content on Server side and sending html data to client every time, I would like to send just JSON from server and then bind the data with the Smarty template on Client side.

This would be very helpful in a way that template would be cached on the client side and then I can reuse it for every AJAX interaction and since I would then be just sending JSON data (and not the entire HTML) from the server, I can save a lot on the data that is actually being downloaded.

I know that mustache templating engine can do that since it's language-agnostic but is it possible to do that with Smarty?

What I don't want is to do something like this in JS

 for (var i in foo) {
    html += '<div class = "myClass">foo[i].bar</div>';
}

Y.one('#myDiv').set('innerHTML', html);

Instead what I want is to have this template of div in the template and then just assign the data to that template

Upvotes: 0

Views: 692

Answers (3)

miroshnikov
miroshnikov

Reputation: 21

There is a port of PHP Smarty to JScript - http://code.google.com/p/jsmart/

It is free and open-source

With it you can use your existing Smarty templates both in PHP and in JavaScript

Upvotes: 2

diagonalbatman
diagonalbatman

Reputation: 18002

I dont think smarty can as it's PHP specific. but try this http://api.jquery.com/category/plugins/templates/ it's a Microsoft contribution to the jquery project

Upvotes: 0

JohnP
JohnP

Reputation: 50019

Smarty works in conjunction with PHP. Since PHP is a server side language you cannot get Smarty to cache client side.

Why not just load the initial structure with placeholders and then for every JSON request, just replace the placeholders in your HTML file?

Upvotes: 0

Related Questions