Server side jQuery templates

Has anyone tried using jQuery templates (or any other JavaScript based templates) on the server side with something like env.js ?

I'm considering trying to figure out what benefits you can get if you can display the same patterns on the client or server side of a web application, but I was hoping that someone might already have some experience or know an existing project was doing this. I would be especially interested to know about any performance issues that I might run into compared to some more traditional template engines.

Recall: Has anyone ever used jquery templates on a server site? If so, were there any performance issues or other issues that I might run into?

+6
javascript jquery templates serverside-javascript envjs
source share
3 answers

env.js not required.

<plug shameless="true">

I am developing and reimplementing jQuery templates so that they can be used independently of the DOM. See https://github.com/mikesamuel/jquery-jquery-tmpl-proposal for code and demos. The spectrum is available at http://wiki.jqueryui.com/w/page/37898666/Template and it says:

Textual, not DOM dependent. Status: Done. See Sections 12. foo $ {bar} translates something very similar to a function (data, parameters) {return "foo" + bar; } modulo some empty bar

...

This will allow you to use this template engine in a server side javascript environment like node.js or java / rhino

I would like to receive feedback and help you get started.

</plug>

+4
source share

My friend, working on a distributed Genetic Programing project, used the js sevrer side-template system to manage all the web workers created in all user browsers. Its code is here: github . I don’t know how useful this will be, but I know that it was quite simple to implement and do some amazing things. From how easily he found this, I would recommend the js template system.

0
source share

It is pretty trivial to write server-side code to handle jQuery templates.

Here is some very simple vb.net code I created that will return the result of a jquery template string to an array of any objects, Currently only replacing data values

 Public Shared Function RenderTemplate(template As String, list As Array) As String Dim myRegexOptions As RegexOptions = RegexOptions.Multiline Dim myRegex As New Regex(strRegex, myRegexOptions) Dim splits = myRegex.Split(template) Dim matches = myRegex.Matches(template) Dim i As Integer = 0 Dim swap As Boolean = False Dim str As New StringBuilder For Each item In list swap = False For i = 0 To splits.Length - 1 If swap Then str.Append(CallByName(item, splits(i), CallType.Get, Nothing)) Else str.Append(splits(i)) End If swap = Not swap Next Next Return str.ToString End Function 

So, if I posted the following ...

 Dim strTargetString As String = "<p><a href='${Link}'>${Name}</a></p>" Dim data As New Generic.List(Of TestClass) data.Add(New TestClass With {.Link = "http://stackoverflow.com", .Name = "First Object"}) data.Add(New TestClass With {.Link = "http://stackexchange.com", .Name = "Second Object"}) Return Render(strTargetString, data.ToArray) 

It prints it as a string

 <p><a href='http://stackoverflow.com'>First Object</a></p> <p><a href='http://stackexchange.com'>Second Object</a></p> 

This will work much faster than creating a false browser object on the server and running the entire jQuery library just to replace a few tags.

0
source share

All Articles