json - 使用 jQuery 和 REST/JSON 简化客户端模板框架

标签 json rest jquery-plugins jquery

关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。












想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。

2年前关闭。




Improve this question




我开始将一些 html 生成任务从服务器端框架迁移到客户端。我在客户端上使用 jQuery。我的目标是通过 REST api 获取 JSON 数据并使用此数据将 HTML 填充到页面中。

现在,当我网站上的用户单击指向 的链接时我的项目 ,服务器生成这样的 HTML:

<dl>
    <dt>Clean Toilet</dt>
    <dd>Get off your butt and clean this filth!</dd>

    <dt>Clean Car</dt>
    <dd>I think there's something growing in there...</dd>

    <dt>Replace Puked on Baby Sheets</dt>
</dl>

我正在更改此设置,以便单击 我的项目 现在将执行返回如下内容的 GET 请求:
[
  {
    "name":"Clean Car",
    "description":"I think there's something growing in there..."
  },
  {
    "name":"Clean Toilets",
    "description":"Get off your butt and clean this filth!"
  },
  {
    "name":"Replace Puked on Baby Sheets"
  }
]

我当然可以编写自定义 jQuery 代码来获取该 JSON 并从中生成 HTML。这不是我的问题,我不需要关于如何做到这一点的建议。

我想做的是将演示文稿和布局与逻辑(jquery 代码)完全分开。我不想通过 jQuery 代码创建 DL、DT 和 DD 元素。我宁愿使用某种可以填充数据的 HTML 模板。这些模板可能只是隐藏在加载应用程序的页面中的 HTML 片段。或者它们可以从服务器动态加载(以支持用户特定的布局、i18n 等)。它们可以显示一次,也可以循环和重复。也许它应该支持子模板,if/then/else,等等。

我的网站上有很多列表和内容,它们以多种不同的方式呈现。我希望创建一种简单且一致的方式来生成和显示内容,而无需为我网站上的每个不同功能创建自定义 jQuery 代码。对我来说,这意味着我需要在 jQuery 之上找到或构建一个满足这些要求的小框架(可能作为插件)。

我发现的唯一一种类似这样的框架是 jTemplates .我不知道它有多好,因为我还没有使用它。乍一看,我对它的模板语法并不感到兴奋。

有人知道我应该研究的其他框架或插件吗?有没有博客文章或其他资源讨论过做这种事情?我只是想确保在自己构建之前已经调查了所有内容。

谢谢!

最佳答案

自从发布这个问题以来,我发现了许多其他模板选项。我在下面列出了其中的许多。然而,最近有一个 jQuery templates proposal这可能是迄今为止最有希望的解决方案。还有一个discussion about it在 jquery 网站上。这是项目位置:

  • https://github.com/nje/jquery/wiki/jquery-templates-proposal

  • 我遇到的其他解决方案包括(排名不分先后):
  • http://www.west-wind.com/weblog/posts/509108.aspx
  • http://ejohn.org/blog/javascript-micro-templating/
  • http://beebole.com/pure/
  • http://archive.plugins.jquery.com/project/jTemplates
  • http://archive.plugins.jquery.com/project/advancedmerge
  • http://archive.plugins.jquery.com/project/tempest
  • http://archive.plugins.jquery.com/project/jBind
  • http://archive.plugins.jquery.com/project/cliche
  • http://archive.plugins.jquery.com/project/appendDom
  • http://archive.plugins.jquery.com/project/openSocial-jquery-templates
  • http://archive.plugins.jquery.com/project/Orange-J
  • http://archive.plugins.jquery.com/project/fromTemplate-microtemplate
  • http://archive.plugins.jquery.com/project/resiglet
  • http://archive.plugins.jquery.com/project/databind
  • http://archive.plugins.jquery.com/project/jsont
  • http://archive.plugins.jquery.com/project/domplate
  • http://archive.plugins.jquery.com/project/noTemplate
  • http://archive.plugins.jquery.com/project/jQueryHtmlTemplates
  • http://github.com/trix/nano
  • http://aefxx.com/jquery-plugins/jqote/
  • http://ajaxian.com/archives/chainjs-jquery-data-binding-service
  • http://ajaxpatterns.org/Browser-Side_Templating
  • http://beebole.com/pure/
  • http://code.google.com/p/google-jstemplate/
  • http://code.google.com/p/trimpath/wiki/JavaScriptTemplates
  • http://embeddedjs.com/
  • Javascript template system - PURE, EJS, jquery plugin?
  • jQuery templating engines
  • http://goessner.net/articles/jsont/
  • 关于json - 使用 jQuery 和 REST/JSON 简化客户端模板框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2215121/

    相关文章:

    java - 如何在 jackson 的反序列化过程中使用注释强制 ACCEPT_SINGLE_VALUE_AS_ARRAY

    c# - 使用 Newtonsoft.JSON 自定义转换器读取具有不同输入的 json

    java - 通过 web xml 进行 Jersey 过滤器注册不起作用

    php - Azure API - PHP 请求

    ruby-on-rails - Rails 更新操作的路由助手是什么?

    jquery - 如何在条件jquery验证插件中显示消息

    json - 如何从Windows从属设备上的groovy文件运行复杂的curl命令(使用json)

    java - 如何使用 Jackson 排除父类(super class)属性

    jQuery: 奇怪的错误: $ ("#dialog").jqm 不是一个函数

    javascript - 这个 jQuery 插件是如何加载数据的?无法从 Firebug 中辨别出来