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.
digiguru
source share