a(href=url, data-active=isActive) label input(type="checkbox", checked=isChecked) | yes / no
详解Node.js模板引擎Jade入门(2)
提供给上面模板的数据:
{
url: "/logout",
isActive: true,
isChecked: false
}
最终渲染后输出的HTML:
<a href="" data-active=" rel="external nofollow" data-active"></a> <label> <input type="checkbox" />yes / no </label>
注意:属性值为false的属性在输出HTML代码时将被忽略;而没有传入属性值时,将默认为true.
5、字面量
为了省事,可以直接在标签名之后写类名和ID名。例如:
div#content
p.lead.center
| Pandora_galen
#side-bar.pull-right // 没有标签名,默认为“div”
span.contact.span4
a(href="/contact" rel="external nofollow" rel="external nofollow" ) contact me
渲染输出的HTML:
<div id="content">
<p class="lead center">
Pandora_galen
<div id="side-bar" class="pull-right"></div>
<span class="contact span4">
<a href="/contact" rel="external nofollow" rel="external nofollow" > contact me </a>
</span>
</div>
6、文本
使用“|”符号输出原始文本。
div | 我两年前开始学习前端 | 在此之间,我学过了html, jQuery, JavaScript, Python, Css3, HTML5, Bootstrap, D3.js, SVG...而现在我在学Node。并且我已经深深的爱上了前端。
7、Script 和 Style块
使用“.”符号在HTML里创建
script.
console.log("Hello world!");
setTiemout(function() {
window.location.href = "http://csdn.net"
}, 1000);
console.log("Good bye!");
生成的代码:
<script>
console.log("Hello world!");
setTiemout(function() {
window.location.href = "http://csdn.net"
}, 1000);
console.log("Good bye!");
</script>
同理,style.生成的是<style></style>。
8、JavaScript代码
使用-,=或!=这三个符号在Jade中写可以操纵输出的可执行JS代码。这在要输出HTML元素和注入JavaScript时是很有用的。不过,这么做一定要小心避免跨站脚本(XSS)的攻击。
比如,可以使用!=定义一个array, 输出标签数据:
- var arr = ['<a>', '<b>', '<c>']
ul
-for (var i = 0; i < arr.length; i++)
li
span= i
span!= "unescaped:" + arr[i] + "vs."
span= "escaped:" + arr[i]
生成的代码如下:
