evantianx / Bloooooooog

Place to record what I thought and learned
0 stars 0 forks source link

Pug #67

Open evantianx opened 6 years ago

evantianx commented 6 years ago

参考文章:

evantianx commented 6 years ago

属性书写

多行属性

多属性之间用空格隔开,若属性名有特殊字符则添加逗号隔开或者加引号

input(
  type='checked'
  name='todo'
  checked
)
<input type="checkbox" name="todo" checked="checked" />

属性插值

- var url = 'test.html';
a(href = '/' + url) Link
<a href="/test.html">Link</a>

支持 ES6 字符串模版语法

属性值编码输出/非编码输出

默认情况下,所有属性值都会编码输出(escaped),即特殊字符会被替换以防止 xss 等攻击。若确实需要特殊字符,用 != 代替 =

div(escaped="<code>")
div(unescaped!="<code>")
<div escaped="&lt;code&gt"></div>
<div unescaped="<code>"></div>

属性值为 boolean 值

按照 HTML 规范走:不写值时为 true。

style 属性

可以传给一个字符串,也可以是一个对象

a(style={text-decoration: 'none', color: 'white'})
a(style="text-decoration: none, color: white")

class 属性 / id 属性

可以传的值有:对象,数组,. + 类名。

. + 类名时,若省略标签名则默认为 div

&attributes

#foo(data-bar="foo")&attributes({'data-foo': 'bar'})
<div id="foo" data-bar="foo" data-foo="bar"></div>
evantianx commented 6 years ago

Case

即 JS 中 switch 的简写。

- var friends = 10

case friends
  when 0
    p  you have no friends
  when 1
    p you have 1 friend
  default
    p you have #{friends} friends
<p>you have 10 friends</p>

若希望某种情况下什么也不输出:

when 0
  - break

也有另外一种写法:

- var friends = 2

case friends
  when 0: p you have no friends
  when 1: p you have 1 friend
  default: p you have #{friends} friends 
evantianx commented 6 years ago

Code

分为 unbuffered codeescaped buffered codeunescaped buffered code

evantianx commented 6 years ago

Comments

分为 buffered commentsunbuffered commentsblock commentsconditional comments

evantianx commented 6 years ago

Conditionals

可以使用 ifelse ifelseunless

evantianx commented 6 years ago

Filters

可以在 pug 模版中使用其他语言,如 markdown、sass、typescript 等,前提是首先安装好对应的 jstransformer

:markdown 类似于这样的语法

evantianx commented 6 years ago

Includes

可以用来引入其他文件

evantianx commented 6 years ago

模版继承

使用 extendsblock 实现继承

也可以用 block append / block prepend 在块后或前添加内容

此时亦可省略 block