JsRender if-else renders nothing

Stephen Collins picture Stephen Collins · Jan 15, 2014 · Viewed 10.6k times · Source

I have a simple model:

var model =  [{
    content: "",
    type: "photo",
    src: "xxx"
}, {
    content: "",
    type: "quote",
    text: "blah"
}];

and a simple template:

{{if type eq='photo'}}
    <img src="{{:src}}" />
    <div class="photo-caption">
        {{:caption}}
    </div>
{{else type eq='quote'}}
    <div class="quote-text">
        {{:text}}
    </div>
{{/if}}

The problem is that the template renders nothing at all when the type is "quote". If I change it slightly to two ifs instead of an if-else, it renders the quote, but also renders the div class="photo-caption">. I need it to only render one or the other. I have a feeling it's a simple matter of syntax, but can't seem to find sufficient docs on how this is done correctly on the JsRender site.

Here's a fiddle. The two templates should behave exactly alike. Instead one renders both, the other renders only the image.

Answer

Steve Wellens picture Steve Wellens · Jan 15, 2014

I got it:

{{if type == 'quote' }}
    <div>
        {{: text }}
    </div>     
{{else type == 'photo'}}   
    <div> 
        {{: src }} 
     </div>   
{{/if}}

http://jsfiddle.net/4QzZX/2/