如何将ejs转换为玉?(How to convert ejs to jade?)

编程入门 行业动态 更新时间:2024-10-24 06:32:59
如何将ejs转换为玉?(How to convert ejs to jade?)

我有麻烦将以下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= message

Note 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.

更多推荐

本文发布于:2023-08-05 17:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1437398.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   如何将   jade   ejs   convert

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!