jQuery templates on the server side
Asked Answered
B

3

6

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

I'm considering attempting it to see what benefits could be gained by being able to render identical templates on either the client or server side of a web application, but I was hoping someone might already have some experience, or know of an existing project doing this. I'd be particularly interested to know about any performance issues I might encounter compared to some more traditional templating engine.

To recap : Has anyone ever used jquery templates on the server site? If so, were there any performance issues, or other problems I might run into?

Behavior answered 16/11, 2010 at 22:56 Comment(5)
What exactly is your question? (see also Is there a template engine for Node.js?)Ragen
Server side is more reliable because Client side you dont know about the users spec, and could dramatically decrease performance on user-endAbrahamsen
You might want to look at @getify's blog (blog.getify.com) - he rants about parity of templating and validation mechanisms between client and server all the timeDemobilize
Ball - To recap : Has anyone ever used jquery templates on the server site? If so, were there any performance issues, or other problems I might run into?Behavior
Just found this answer that suggests 2 jQuery template renderers in PHP and .Net respectively ... #4485919Antilles
A
4

env.js is unnecessary.

<plug shameless="true">

I am in the process of specing and re-implementing JQuery templates to allow them to be used independently of the DOM. See https://github.com/mikesamuel/jquery-jquery-tmpl-proposal for code, and demos. The spec is available at http://wiki.jqueryui.com/w/page/37898666/Template and it says:

Text-centric rather than DOM dependent. Status: Done. See section 12 implementations. foo${bar} translates to something very similar to function (data, options) { return "foo" + bar; } modulo some dethunking of bar

...

This will allow to use this template engine in server side javascript environment, such as node.js, or java/rhino

I would love feedback and can help you get started.

</plug>

Arapaho answered 26/5, 2011 at 22:43 Comment(0)
Y
0

A friend of mine working on a distributed Genetic Programing project used a js sevrer side template system to manage all the web workers spawned across all users browsers. His code is here: github. I don't know how helpful it will be, but I know it was quite simple to implement and did some amazing things. From how easy he found it I would recommend a js template system it.

Yabber answered 17/11, 2010 at 7:44 Comment(0)
A
0

It's fairly trivial to write server side code to process the jQuery templates.

Here is some very basic vb.net code I have created that will return the result of a jquery template string to an array of any objects. Currently it only does the replacement of 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 sent in 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 would output 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 alot faster than spawning up a fake browser object on the server, and running the whole jQuery library just to replace a few tags.

Antilles answered 26/5, 2011 at 21:58 Comment(1)
It's easy to render the simplest case templates, but what happens when the templates include function calls to JavaScript functions (which is not uncommon in real-world scenarios)? That's tough without actually using JavaScript on the server-side.Girhiny

© 2022 - 2024 — McMap. All rights reserved.