我有麻烦将以下ejs转换为jade:
<% if (message.length > 0) { %> <div class="alert alert-danger"><%= message %></div> <% } %>这个块怎么看玉?
I have troubles converting the following ejs to jade:
<% if (message.length > 0) { %> <div class="alert alert-danger"><%= message %></div> <% } %>How does this block look in jade?
最满意答案
我写的如下:
if message.length .alert.alert-danger= message...因为message是一个字符串,只有当它是一个空字符串时, message.length才会计算为一个假值(0)。 我在这里也使用了buffered output (注意=字符),以便message值被HTML转义。
另一种方法是直接将message与空字符串进行比较:
if message !== '' .alert.alert-danger= message请注意,我在div.alert.alert-danger表达式中删除了div ,因为它是Jade模板的默认元素。 您是否使用过任何其他元素,您必须使用其标记名启动表达式。
I'd write this as follows:
if message.length .alert.alert-danger= message... as message is a string, message.length evaluates to a falsy value (0) only if it's an empty string. I've also used buffered output here (note that = character) so that message value will be HTML escaped.
An alternative approach is comparing message with an empty string directly:
if message !== '' .alert.alert-danger= messageNote that I dropped div in div.alert.alert-danger expression, as it's kind of default element for Jade templates. Have you used any other element, you'd have to start the expression with its tagname.
更多推荐
发布评论