服务器端的 jQuery 模板

发布于 2024-10-03 01:03:44 字数 395 浏览 1 评论 0原文

有没有人尝试在服务器上使用 jQuery 模板 (或任何其他基于 JavaScript 的模板)与诸如 env.js 之类的东西站在一边?

我正在考虑尝试它,看看通过在 Web 应用程序的客户端或服务器端呈现相同的模板可以获得什么好处,但我希望有人可能已经有一些经验,或者知道现有的项目正在做这。我特别有兴趣了解与一些更传统的模板引擎相比我可能遇到的任何性能问题。

回顾一下:有人曾经在服务器站点上使用过 jquery 模板吗?如果是这样,是否存在任何性能问题或我可能遇到的其他问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

亽野灬性zι浪 2024-10-10 01:03:44

env.js 是不必要的。

我正在规范和重新实现 JQuery 模板,以允许它们独立于 DOM 使用。请参阅https://github.com/mikesamuel/jquery-jquery-tmpl-proposal 用于代码和演示。该规范位于 http://wiki.jqueryui.com/w/page/37898666 /Template 它说:

以文本为中心而不是依赖 DOM。状态:完成。参见第 12 节的实现。 foo${bar} 翻译成与 function (data, options) { return "foo" + bar; 非常相似的东西} 对 bar 进行取模

...

这将允许在服务器端 javascript 环境中使用此模板引擎,例如 node.js 或 java/rhino

我希望获得反馈并可以帮助您入门。

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>

无声静候 2024-10-10 01:03:44

我的一位朋友从事分布式遗传编程项目,他使用 js sevrer 端模板系统来管理所有用户浏览器上生成的所有 Web Worker。他的代码在这里:github。我不知道它会有多大帮助,但我知道它实施起来非常简单,并且做了一些令人惊奇的事情。从他发现它有多容易我会推荐一个 js 模板系统。

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.

各自安好 2024-10-10 01:03:44

编写服务器端代码来处理 jQuery 模板相当简单。

这是我创建的一些非常基本的vb.net代码,它将返回jquery 模板字符串的结果到任何对象的数组。目前它只替换数据值

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

所以如果我发送以下内容...

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)

它会将其输出为字符串

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

这比在服务器上生成一个假浏览器对象并运行整个 jQuery 库要快得多替换一些标签。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文