Skip to content Skip to sidebar Skip to footer

Outputting Directly To Template With Embedded Javascript Syntax

I'm using Backbone with Underscore templates. I have a JavaScript if() condition in my code that looks something like this:
<% if(somevalue === tru

Solution 1:

There are a few options, you could use <%= %> and a ternary:

<%= somevalue == true ? 'your face' : 'my face' %>

or you could use print:

You can also use print from within JavaScript code. This is sometimes more convenient than using <%= ... %>.

var compiled = _.template("<% print('Hello ' + epithet); %>");
compiled({epithet:"stooge"});
=> "Hello stooge."

so you could do this:

<% if(somevalue == true) { 
    print("your face");
} else { 
    print("my face");
} %>

I'm assuming that if(somevalue = true) is a typo and should be if(somevalue == true).

Post a Comment for "Outputting Directly To Template With Embedded Javascript Syntax"