该博客首发于www.litreily.top
Pug – robust, elegant, feature rich template engine for Node.js
pug
原名jade
,因版权问题更名为pug
,即哈巴狗。与hexo
默认模块ejs
一样,pug
也是一个模板引擎,可用于快速的网站开发,当然也可以用于静态博客网站的设计。本站点现时所用主题manupassant
也使用了pug
。
本文针对Hexo
中使用pug
的情况为例,说明其基本语法。
安装
# common install
npm install pug
# install for hexo blog
npm install hexo-renderer-pug --save
复制代码
语法
pug
不同于html
,前者不需要标签的开和闭,如html
的<p>Demo</p>
,在pug
使用p Demo
即可。
缩进
pug
对空格敏感,有点类似python
对制表符tab
敏感。pug
使用空格作为缩进符,当然用soft tab也可行。同一级标签需保证左对齐。
div
p Hello, world!
p Hello, pug.
复制代码
渲染结果如下:
<div>
<p>Hellow, world!</p>
<p>Hello, pug.</p>
</div>
复制代码
注释
pug
使用//-
或//
对代码进行注释,前者注释内容不出现在渲染后的html
文件中,后者反之。
//- html中不包含此行
// html中会包含此行
复制代码
属性
pug
将标签属性存放于括号()
内,多个属性之间以逗号或空格分隔。此外,对于标签的id
和class
,pug
使用#
紧跟标签id
,使用.
紧跟标签class
,可以同时设置多个class
。
h1#title Test title
img#name.class1.class2(src="/test.png" alt="test")
复制代码
↓
<h1 id="title">Test title</h1>
<img