更好的方式在ucfirst在Jade模板?
有没有更好的方法来大写string的第一个字符在Jade比这个?
for list in project.lists - list.name = list.name.charAt(0).toUpperCase() + list.name.slice(1); li #{list.name}
每次我想要大写一个variables都这样做是丑陋的,有没有什么方法在Jade中,我可以定义一个自定义函数,我可以在每个模板中使用:
for list in project.lists li #{ucfirst(list.name)}
提前致谢!
#{}
的内容是作为标准的JS执行的,所以你可以传入辅助函数来用于类似的事情。 你没有指定,但假设你使用Jade和Express,你可以这样做:
app.locals.ucfirst = function(value){ return value.charAt(0).toUpperCase() + value.slice(1); };
这将在Jade模板中公开一个名为ucfirst
的函数。 您也可以在每次渲染时将其作为locals
一部分传递,但是如果您使用的是Express,则会自动执行此操作。
如果你愿意诉诸CSS,你可以创build一个类,使目标元素中的每个单词的第一个字母大写。
CSS
.caps { text-transform: capitalize; }
玉
div.caps each foo in ['one', 'two', 'three'] span #{foo}
生成的HTML
<div class="caps"><span>one</span><span>two</span><span>three</span>
结果视图
One Two Three
如果你正在使用帕格,这可能是有帮助的:
mixin ucfirst(text) - text = text.charAt(0).toUpperCase() + text.slice(1); . #{text}
只要把它叫做任何其他的mixin:
li +ucfirst(list.name)