3

I'm trying to develop a web application that it obtains information from a server (in a mysql database ) and it is shown to the client (via browser) this information.

I want to use backbone.js and the default templates system (underscore.js) in the client part. For other hand, I want to use jsp for accessing to information from the mysql database in the server.

My problem is that I don't know so fine if it's possible to combine jsp and underscore.js (independently of syntax's problems associated to the fact what <% %> structure is the same to both technologies of).

I have searched about it, but I haven't found any example that use both technologies.

it's possible to combine both? or why not? can somebody show me some example?

Thanks in advance!!

MODIFIED AS NEXT:

I'm aware of syntax problem's existence, but I don't understand as to combine both (jsp and undescore.js). If I have the next template (underscore.js) in a html file:

<!-- 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>

And I have a backbone's view that replace the 'title', 'author' and 'description' fields of the template with the corresponding values:

<!-- 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;
  }
});

How can I do for getting that value of a 'title', 'author' and 'description' fields is obtained from server via jsp?

mol
  • 153
  • 3
  • 12
  • JSP runs on the server. It (usually) emits HTML. Underscore.js runs on the client - either from JavaScript emitted in the HTML or loaded script elements. They are not related and should be viewed as two *separate* systems, with JSP feeding into the other. Now, dealing with the literals, that might get fugly .. but it's only related to JSP and how it generates the emitted HTML. – user2246674 May 04 '13 at 19:51
  • In the (JSP) HTML markup, it would be sufficient to use `<% other underscore template stuff %>`, perhaps? – user2246674 May 04 '13 at 19:56
  • 2
    Might be easier to use different delimiters with Underscore, see [`_.templateSettings`](http://underscorejs.org/#template). – mu is too short May 04 '13 at 20:15
  • I'm aware syntax's problem, but I don't undestand as to combina both. If I have next template: – mol May 05 '13 at 11:47
  • Thanks for your responses! I keep going without understanding it so fine, I have added an example of the doubt I have – mol May 05 '13 at 12:16
  • Possible duplicate: [Underscore.js Templates Within JSP](http://stackoverflow.com/questions/5771742/underscore-js-templates-within-jsp) – Vik David Nov 19 '13 at 10:24

1 Answers1

0

Here is new _.temeplateSettings for backbone.js

_.templateSettings = {
    interpolate: /\{\{(.+?)\}\}/gim,
    evaluate: /\{\{(.+?)\}\}/gim,
    escape: /\{\{\-(.+?)\}\}/gim
};

template syntax:

<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>