凸显了模版条件 [英] Underscore Conditionals in Template

查看:134
本文介绍了凸显了模版条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的模板:

var tmpl = _.template('<<%= elementTag %> <% if (_.has(elementClass)) { %> class="<%= elementClass %>" <% } %> <%= elementExtra %>><%= template(elementContent) %></<%= elementTag %>>');

有的,我将呈现使用该模板对象我没有一个 elementClass中属性设置。我试图用 _。有这样模版只尝试打印为其中 elementClass中为对象的属性定义,但我没有成功。控制台错误状态仅 elementClass中没有设置,presumably由于条件语句失败,因为我本来打算工作。

Some of the objects that I will render using this template my not have an elementClass attribute set. I've tried to use _.has so that the template only attempts to print the attribute for objects where elementClass is defined but I have had no success. The console error states only that elementClass is not set, presumably because the conditional statement failed to work as I had intended.

我知道这是一个简单的问题,但我似乎无法来解决吗? - 我该如何使用条件在这样的语句来检测没有一定的属性的对象设置

I know this is a simple problem, but I can't seem to solve it - how can I use conditionals in statements like this to detect objects without certain attributes set?

推荐答案

您不能,一般来说,使用 _。isDefined 这一点。考虑这个模板:

You can't, in general, use _.isDefined for this. Consider this template:

<script id="t" type="text/x-underscore-template">
    <% if(!_.isUndefined(elementClass)) { %>
        <%= elementClass %>
    <% } else { %>
        No such thing.
    <% } %>
</script>

这code:

var t = _.template($('#t').html());
console.log(t({ }));

T 的调用应该给你的ReferenceError因为没有 elementClass中的范围为编译模板函数。所以,如果你的对象不具有<​​code> elementClass中属性的话,那么该模板功能,当你试图让你的模板失败。

The call to t should give you a ReferenceError because there is no elementClass in scope for the compiled template function. So, if your objects don't have an elementClass property at all, then the template function will fail when you try to get your template.

演示: http://jsfiddle.net/ambiguous/H22D2/

问题是, _。isUndefined 功能只能检查的的一名前pression的,它不能检查变量(或财产因模板的内部 使用)你传递给它已被宣布。

The problem is that the _.isUndefined function can only check the value of an expression, it cannot check if the variable (or property due to the template's internal with usage) you're passing to it has been declared.

如果对象将有一个 elementClass中属性,但它可能有一个未定义的值,那么你可以使用 _。isDefined

If the objects will have an elementClass property but it might have an undefined value then you can use _.isDefined and this:

t({ elementClass: undefined })

就可以了。

演示: http://jsfiddle.net/ambiguous/LUakg/

如果对象可能会或可能不会有 elementClass中属性,然后你使用的 的typeof 的typeof 并不试图评估其操作数。事情是这样的:

If the objects might or might not have elementClass properties, then you're stuck using typeof as typeof doesn't try to evaluate its operand. Something like this:

<script id="t" type="text/x-underscore-template">
    <% if(typeof elementClass !== 'undefined') { %>
        <%= elementClass %>
    <% } else { %>
        No such thing.
    <% } %>
</script>

将在这三种情况下感兴趣的正常工作:

will work fine in all three cases of interest:

var t = _.template($('#t').html());
console.log(t({ }));
console.log(t({ elementClass: undefined }));
console.log(t({ elementClass: 'pancakes' }));

演示: http://jsfiddle.net/ambiguous/H5xC7/

这篇关于凸显了模版条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆