Petran
Petran

Reputation: 8047

backbone alternative syntax

I come from php where I can right alternative syntax if else statements like

<?php if(): ?>
    // conditional html goes here
<?php endif;?>

My question is if there is a way to do the same thing on a backbone view like

<% if(condition): %>
    html code goes here ;

<% endif; %>

Upvotes: 0

Views: 189

Answers (2)

mu is too short
mu is too short

Reputation: 434595

If you're using the default Underscore templates then:

<% if(condition) { %>
    HTML goes here
<% } %>

Don't forget the { and } or you'll wind up with a mess.

Demo: http://jsfiddle.net/ambiguous/W33Tw/

Upvotes: 1

Andbdrew
Andbdrew

Reputation: 11895

This question should really be tagged with whatever template solution you are using with backbone. If you are using handlebars (which is awesome http://handlebarsjs.com/ ), you can do that with:

{{#if contextPropertyThatEvaluatesSanelyAsABoolean}}
  your html!
{{else}}
  different html!
{{/if}}

and then pass in a context like:

$(@el).html(this.template({
  contextPropertyThatEvaluatesSanelyAsABoolean: true
}))

or if you are using coffeescript:

$(@el).html @template
  contextPropertyThatEvaluatesSanelyAsABoolean: true

There are similar solutions for other templating languages too.

Upvotes: 0

Related Questions