copenndthagen
copenndthagen

Reputation: 50732

Underscore JS Add logic in template

I am passing the below to my Underscrore template (now fieldAttr is dynamic...can have 2 attrs, 3 attrs, etc)

fieldAttr: {
    id: "myElmId",
    dataModelId: $(this).attr('data-model-id')
}

Now in my HTML template, I want to loop and set finalAttributesString (which i'll use to set my textarea attributes)

<% for (var i = 0, i < fieldAttr.length; i++) { %>
    //logic to get finalAttributesString
<% } %>

<textarea <%=finalAttributesString%>></textarea>

So it should render as below:

<textarea id="myElmId" data-model-id="123"></textarea>

Can I do this using Underscore JS?

Upvotes: 3

Views: 139

Answers (1)

TKrugg
TKrugg

Reputation: 2405

It much easier to do this

<textarea <% for(var attr in fieldAttr) { %> <%= attr %>="<%= fieldAttr[attr] %>" <% } %> 
></textarea>

I made a jsfiddle here

Upvotes: 1

Related Questions