Senthil
Senthil

Reputation: 956

what is the use of <%% in underscore templates?

can some one tell me difference between "<%%" "<%"

<%%= hello %>

<%= hello %>

i could not find proper answer in google search.

Any explanations will be helpful :)

**Index.html**

<div id="container">Loading...</div>
<script type="script/template" id="hello_sen">
<%= hello %>
</script>

**Backbone View**

class Bckbone.Views.EntriesIndex extends Backbone.View

initialize: ->
    @template = _.template($("#hello_sen").html())

render: ->
    datas = {hello: "Senthil"}
    $(@el).html(@template(datas))
    this

Upvotes: 0

Views: 1843

Answers (3)

Greg Blass
Greg Blass

Reputation: 3660

My preferred solution: Move the template to a partial, and don't include the .erb after .html in the filename. Then rails won't parse ERB in that file.

Upvotes: 0

Connor Leech
Connor Leech

Reputation: 18873

Backbone.js relies on underscore.js for templating. <% is convention in underscore. <%% escapes the ERB tags for rails. You can change underscore's settings:

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

Or use <%% to escape on a line by line basis. Escaping still ends with %>

More here: Rails with Underscore.js Templates

Upvotes: 0

Dogbert
Dogbert

Reputation: 222398

You're getting the error in the screenshot you posted above because you're using erb style underscore template (the default) inside an erb file.

The code inside <% and %> is being parsed as Ruby code.

You should use alternate interpolation strings, as described here.

Upvotes: 1

Related Questions