我正在尝试开发一个 Web 应用程序,它从服务器(在 mysql 数据库中)获取信息,并(通过浏览器)向客户端显示此信息。
我想在客户端使用 backbone.js 和默认模板系统 (underscore.js)。另一方面,我想使用 jsp 从服务器中的 mysql 数据库访问信息。
我的问题是我不知道是否可以将 jsp 和 underscore.js 结合起来(独立于 <% %> 结构与这两种技术相同这一事实相关的语法问题)。
我已经搜索过了,但我还没有找到任何使用这两种技术的例子。
两者可以结合吗?或者为什么不呢? 有人可以给我举个例子吗?
提前致谢!
修改为下一个:
我知道语法问题的存在,但我不明白如何将两者结合起来(jsp 和 undescore.js)。如果我在 html 文件中有下一个模板 (underscore.js):
<!-- language: lang-js -->
<script type="text/template" id="showTemplate">
<h2>
<%= title %>
<small>by: <%= author %></small>
</h2>
<p style="white-space:pre-wrap;"><%= description %></p>
</script>
我有一个主干 View ,用相应的值替换模板的“标题”、“作者”和“描述”字段:
<!-- language: lang-js -->
viewExample = Backbone.View.extend({
// the constructor
initialize: function (options) {
this.note = options.note;
},
// populate the html to the dom
render: function () {
this.$el.html(_.template($('#formTemplate').html(), {title: "Environment", author:"Albert", description:"textDescription"}));
return this;
}
});
如何通过 jsp 从服务器获取“标题”、“作者”和“描述”字段的值?
最佳答案
这是 backbone.js 的新 _.temeplateSettings
_.templateSettings = {
interpolate: /\{\{(.+?)\}\}/gim,
evaluate: /\{\{(.+?)\}\}/gim,
escape: /\{\{\-(.+?)\}\}/gim
};
模板语法:
<script type="text/template" id="item-template">
<div class="view">
<input class="toggle" type="checkbox" {{ done ? 'checked="checked"' : '' }} />
<label>{{ title }}</label>
<a class="destroy"></a>
</div>
<input class="edit" type="text" value="{{ title }}" />
</script>
关于jsp - 结合 UNDERSCORE.JS 和 JSP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16378289/