admin管理员组文章数量:1401922
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 if
s 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.
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 if
s 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.
Share Improve this question edited Jan 15, 2014 at 2:39 Stephen Collins asked Jan 15, 2014 at 1:31 Stephen CollinsStephen Collins 3,6539 gold badges41 silver badges63 bronze badges 1- If you make a jsFiddle, you'll have a much better chance of being helped. – Steve Wellens Commented Jan 15, 2014 at 2:15
2 Answers
Reset to default 5I got it:
{{if type == 'quote' }}
<div>
{{: text }}
</div>
{{else type == 'photo'}}
<div>
{{: src }}
</div>
{{/if}}
http://jsfiddle/4QzZX/2/
When you tried the version with two if statements, did you remember to add a {{/if}}
in addition to changing the {{else}}
into an {{if}}
?
{{if type eq='photo'}}
<img src="{{:src}}" />
<div class="photo-caption">
{{:caption}}
</div>
{{/if}}
{{if type eq='quote'}}
<div class="quote-text">
{{:text}}
</div>
{{/if}}
本文标签: javascriptJsRender ifelse renders nothingStack Overflow
版权声明:本文标题:javascript - JsRender if-else renders nothing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744248123a2597119.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论