简体   繁体   English

服务器端模板,客户端模板-自动转换?

[英]Server side templates, client side templates - Automatic conversion?

With the event of the jQuery templates plugin, I am wondering how to maintain both server-side and client-side templates. 有了jQuery模板插件,我想知道如何同时维护服务器端和客户端模板。

Because in my case, I have some templates, that would be duplicates - server-side as well as client-side. 因为就我而言,我有一些模板,它们是重复的-服务器端和客户端。 Just imagine this: 试想一下:

You have a form, where you can enter list points. 您有一个表单,可以在其中输入列表点。 When you press submit, an AJAX request is generated but the entry is already showed on your page. 当您按下提交时,会生成一个AJAX请求,但该条目已经显示在页面上。

Now, the markup for this bullet point should be the same - wether it was generated through JS or by my PHP/.NET/whatsoever application. 现在,此要点的标记应该是相同的-无论是通过JS还是由我的PHP / .NET /任何应用程序生成的。

Are there any good ways to maintain those templates / keep them in sync? 有什么好的方法来维护这些模板/使其保持同步? Or do I really have to manually maintain both templates? 还是我真的必须手动维护两个模板?

What is your experience in applications using JS templates? 您在使用JS模板的应用程序方面有什么经验?

If you like JS->PHP priority :) then you have this two jquery-tmpl compatible template renderer for PHP backend 如果您喜欢JS-> PHP优先级 :),那么您有两个兼容PHP后端的jquery-tmpl模板渲染器

  1. https://github.com/abackstrom/jquery-tmpl-php https://github.com/abackstrom/jquery-tmpl-php
  2. https://github.com/xyu/jquery-tmpl-php https://github.com/xyu/jquery-tmpl-php

If you prefer more PHP->JS priority :) then you can try this Javascript implementation of popular PHP templating Smarty 如果您更喜欢PHP-> JS优先级 :),则可以尝试使用这种流行的PHP模板Smarty的Javascript实现。

  1. http://code.google.com/p/jsmart/ http://code.google.com/p/jsmart/

Or you can try something more neutral like: 或者您可以尝试更中性的方法,例如:

  1. Mustache http://mustache.github.com/ 小胡子http://mustache.github.com/

My coworker is the author of the .NET parser you mention, so I've used it quite a bit to share templates between the client and server. 我的同事是您提到的.NET解析器的作者,因此我已经用了很多时间在客户端和服务器之间共享模板。 To be clear - it's the same template, same file no matter whether it's being parsed on the client or server. 明确地说,它是相同的模板,相同的文件,无论是在客户端还是在服务器上进行解析。 So no duplication/maintenance headaches, which is very nice. 因此,没有重复/维护的麻烦,这非常好。

The only caveat we've run into is advanced logic. 我们遇到的唯一警告是高级逻辑。 The data that the template renders needs to be more or less print-ready at the time of rendering. 模板渲染的数据在渲染时需要或多或少准备打印。 So checks for array lengths need to be booleans with the result, strings need to be pluralized, dates need to be formatted, etc. I believe Aaron is working on a way to pass functions in as named parameters to the .NET version, so that eg a pluralize function available in the scope of the template on the client-side could be replicated in C# and used in the same way on the server, allowing more logic and processing to happen within the template. 因此,对数组长度的检查必须是布尔值,结果字符串需要复数,日期需要格式化,等等。我相信Aaron正在研究一种将函数作为命名参数传递给.NET版本的方法,以便例如,可以在C#中复制在客户端模板范围内可用的复数功能,并在服务器上以相同的方式使用,从而允许在模板中进行更多的逻辑和处理。 Still, even having to pre-process the data, it's been incredibly useful. 尽管如此,即使必须对数据进行预处理,它还是非常有用的。

The Spark View Engine (.NET) has a javascript rendering functionnality: http://blog.robertgreyling.com/2009/11/teaching-javascript-how-to-render-your.html Spark View Engine(.NET)具有javascript渲染功能: http : //blog.robertgreyling.com/2009/11/teaching-javascript-how-to-render-your.html

Another way to have only one set of templates is to do only server side rendering, using ajax to get the updated html from the server. 仅拥有一组模板的另一种方法是仅执行服务器端渲染,即使用ajax从服务器获取更新的html。

I usually store them on the view ; 我通常将它们存储在视图上 ; here I'm going to tell you about a really interesting use-case I found for jquery-tmpl . 在这里,我将告诉您有关jquery-tmpl一个非常有趣的用例。

I've used jquery-tmpl on a site that, due to the huge amount of requests, required a technique I call decontextualization . 我在一个站点上使用了jquery-tmpl ,由于存在大量请求,因此需要一种我称为decontextualization的技术。 This technique was implemented just to stay alive during peak hours and it consists purely on the following rules: 实施此技术只是为了在高峰时段保持生命,它完全由以下规则组成:

  1. Never touch the server to regenerate the page more than once; 切勿触摸服务器以多次重新生成页面。 unless it's absolutely necesary. 除非绝对必要。
  2. Use JavaScript to provide a state of who the user is and what privileges he has. 使用JavaScript来提供用户是 ,什么特权,他有一个状态。

With those two rules in mind you may notice that jquery-tmpl with the basic amount of logic it provided was simply majestic for the given case. 考虑到这两个规则,您可能会注意到, jquery-tmpl具有提供的基本逻辑量,对于给定情况而言简直是雄伟。 What I did basically is including the jquery-tmpl templates into the documents that required to be descontextualized. 我所做的基本上是将jquery-tmpl模板包含在需要去上下文化的文档中。 All the templates were provided by the page itself; 所有模板均由页面本身提供; so I could make a d18n javascript library which could do the following: 所以我可以制作一个d18n javascript库,该库可以执行以下操作:

  1. Query a really fast script about current user, return data as a JSON object. 查询有关当前用户的快速脚本,将数据作为JSON对象返回。
  2. Traverse the JSON and include the templates provided on the document on the specified selectors. 遍历JSON,并在指定选择器上包括文档中提供的模板。 Let jquery-tmpl do the math. jquery-tmpl进行数学运算。

Whenever we needed to do a modification to the "tempalte" we would do it the same way as we would do it if there was no jquery-tmpl available: ON THE VIEW / PARTIAL . 每当我们需要对“ tempalte”进行修改时,我们都将采用与没有可用的jquery-tmpl相同的方式jquery-tmplON VIEW / PARTIAL

You would view something like this for a "can user edit post?" 您将为“用户可以编辑帖子”查看类似内容。 template: 模板:

<script id="post-edit-button" type="text/x-jquery-tmpl">
  {{if user_id == "<%= post.user.id %>" || role == "staff" || $.inArray(user_id, EDITORS) }}
    <a href="<%= edit_post_url(post) %>">Edit</a>
  {{/if}}
</script>

Hope my experience is useful for you at some degree. 希望我的经验对您有所帮助。

I use XSLT for this. 我为此使用XSLT。 Not everyone's a fan of its syntax, but it is crossbrowser, fast, and works - and you can even have client-side templating without much if any scripting at all by using a form targetting an embedded iframe - to have the advantage of small over-the wire documents (slightly larger than json, but not much). 不是每个人的其语法的粉丝,但它跨浏览器,速度快,而且作品-你甚至可以让客户端模板没有太多的如果在所有任何脚本使用形式靶向嵌入式IFRAME -有小过的优势-wire文档(比json稍大,但不多)。 iOS devices don't support it; iOS设备不支持它。 these would need to use the server-templated version - but the nice thing is that that's trivially possible with the same templates. 这些将需要使用服务器模板版本-但可喜的是,使用相同的模板几乎可以做到。

I'm not a huge fan of javascript templating libraries: they're complex, version-dependant, generally have poor tooling, liable to break as browsers change, and usually tie you to particular javascript libraries. 我不是javascript模板库的忠实拥护者:它们非常复杂,依赖于版本,通常使用的工具较差,容易随浏览器的更改而中断,并且通常将您绑定到特定的javascript库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM