Emil A.
Emil A.

Reputation: 3445

Underscore template <script type="text/template></script> within another template

I'm using underscore + express to render a web page, here's an example of a template:

<!doctype>
<html>
<head>
    <meta charset="utf-8">
    <title><%= title %></title>
</head>
<body>
    <div class="description"><%= description %></div>
</body>
</html>

Everything good so far, however I'd like to use the following markup within the page:

<script type="text/template">
    <div class="foo"><%= bar %></div>
</script>

If I'll use it, then the server-side underscore will try to parse it and that's not my intention, I'd like to use _.template on the client side as well.

Is there any way I can ignore some parts of the template? I went through the docs, but I didn't find anything . Something like this would be really useful:

<%! %>

Example:

<%!
<script type="text/template">
    <%= text %>
</script>
%>

I know I can compile the page in parts, but is there a better solution?

Upvotes: 0

Views: 797

Answers (1)

David Sulc
David Sulc

Reputation: 25994

You can set your client-side template settings to use different interpolation settings so they don't conflict with the server side ones. See http://underscorejs.org/#template

_.templateSettings = {
  interpolate: /<%!(.+?)%>/g
};

Note you might have to escape some of the characters.

Upvotes: 2

Related Questions