AnApprentice
AnApprentice

Reputation: 110960

Using an If condition inside a template

How do you use an IF statement inside of a jQuery template?

Here's my template:

<script type="text/html" id="attachmentListTemplate">
{{if $item.current_cmt_id == id }}
    <li>
        <a href="${link}" class="fancyIFrame clearfix">${title}</a>
    </li>
{{/if}}
</script>

Where id is essentially ${id} and is being passed by the data binding (via KnockoutJS). Which without an IF statement outputs fine, like so: ${$item.current_cmt_id}

Here is the data-binding (powered by KnockoutJS):

<ul data-bind='template: { name: "attachmentListTemplate", foreach: attachmentsModel.convAttachments, templateOptions: {current_cmt_id: <%=comment.id%>} }'> </ul>

Any suggestions as to why the if sttatement is not working? Am I comparing the two conditions correctly?

Upvotes: 2

Views: 5156

Answers (2)

Luke Bennett
Luke Bennett

Reputation: 32896

Assuming that id is an observable, you need to invoke it as a function rather than treat is as a property. Try the following:

{{if $item.current_cmt_id == id()}}

Upvotes: 6

Erick Smith
Erick Smith

Reputation: 930

Do you need to surround the <%=comment.id%> with quotes?

        <ul data-bind='template: { name: "attachmentListTemplate", foreach: attachmentsModel.convAttachments, templateOptions: {current_cmt_id: "<%=comment.id%>"} }'> </ul>

Upvotes: 0

Related Questions