
Client-side templating is already a reality
Background
I am developing IFrame applications for VKontakte social network. The most convenient way to navigate the application is to dynamically load data without reloading the entire page. I used to generate html code to be displayed on the server, until I met EJS - JavaScript Templates ...
EJS - Embedded JavaScript
EJS turned out to be one of the most convenient and suitable template engines for me. It works with both single variables and arrays (read objects), there is logic (if ... else ...).
Let me show you with an example what this template engine can do.
Template - /templates/question.ejs:
<% if(question) { %>
<%= author %>: <%= question %>
<% } else { %>
Нет вопросов, на которые можно ответить!
<% } %>
* This source code was highlighted with Source Code Highlighter.
Data - /data/question.php:
{"id":"98","question":"What are you doing now?","author":"Mihalich88"}
* This source code was highlighted with Source Code Highlighter.
Result:
$.ajax({
type: "POST",
url: "/data/question.php",
dataType: "json",
data: data,
success: function(ans){
var html = new EJS({url: ' /templates/question.ejs'}).render(ans);
}
});
* This source code was highlighted with Source Code Highlighter.
Eventually
+ "Pluses"
- Saving traffic, because only data is transmitted in the form of a json object, the template is cached and taken from the cache
- Reducing the load on the server, because templating occurs in the browser
- MVC structure, with the output of "V" outside the server
- Works even on Opera Mobile 10
- "minuses"
- Not indexable, but perfect for RIA
An example of an application using EJS: http://formspring.vk-app.ru Pitfall
: the template engine only works with files that have the * .ejs extension . I wanted the templates to have the * .tpl extension, but it didn’t work out - it hangs on the rendering ... Perhaps, if you pick it up, it will work.