pug模版學習(一)

標簽

按照html的縮進格式

doctype html
html
  head
    title
  body

編譯結果:

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body><body>
</html>

文本

p 這是文本
| 這是文本
p.
  這是文本

編譯結果:

<p>這是文本</p>
這是文本
<p>這是文本</p>

屬性

設置class名跟id名(默認是div)

p.foo
p#foo
p#foo.foo
.foo
#foo

編譯結果:

<p class="foo"></p>
<p id="foo"></p>
<p id="foo" class="foo"></p>
<div class="foo"></div>
<div id="foo"></div>

其他屬性:

a(href="google.com") google
- var foo = true
p(class=foo ? "authed" : "anon")
input(
  type="checkbox"
  name="agreement"
  checked
)
div(data-bar="foo")&attributes({"data-foo": "bar"})

- var attr = {}
- attr.class = "baz"
div(data-bar="foo")&attributes(attr)
        

編譯結果:

<a href="google.com">google</a>
<p class="authed"></p>
<input type="checkbox" name="agreement" checked>
<div data-bar="foo" data-foo="bar"></div>

<div class="baz" data-bar="foo"></div>

注釋

// 行注釋
//- 編譯后不會顯示出來
//
  塊注釋
<!--可以直接使用注釋代碼-->

case語句

- var num = 3
case num
  when 0
    p 這是0
  when 1 
    - break
  when 3
    p 這是#{num}

編譯結果:

<p>這是3</p>

循環

ul
  - var n = 0
  while n < 4
    li= n++
 ul
  - for (var x = 0; x < 3; x++)
    li item
ol
  - var list = [1,2,3,4,5,6]
  each item in list
    li= item

編譯結果:

<ul>
  <li>0</li>
  <li>1</li>
  <li>2</li>
  <li>3</li>
</ul>
<ul>
  <li>item</li>
  <li>item</li>
  <li>item</li>
</ul>
<ol>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
  <li>6</li>
</ol>

條件語句

- var user = {foo: "this is foo"}
- var bar = baz
#user
  if user.foo
    h2.green foo
    p.foo= user.foo
  else if bar
    h2.blue foo
    p.foo.
      User has no foo
  else
    h2.red foo
    p.foo user has no foo

unless user.isFoo
  p 等同于:if !user.Foo

編譯結果:

<div id="user">
  <h2 class="green">foo</h2>
  <p class="foo">this is foo</p>
  <p>等同于:if !user.Foo</p>
</div>

mixin 創建重用的代碼

mixin list
  ul
    li foo
    li bar 
    li baz
+list
+list

編譯以后:

<ul>
  <li>foo</li>
  <li>bar </li>
  <li>baz</li>
</ul>
<ul>
  <li>foo</li>
  <li>bar </li>
  <li>baz</li>
</ul>

mixin支持傳參

mixin article(title)
  .article
    .article-wrapper
      h3= title
      if block
        block
      else
        p NO content provided
+article("Hello worle")
+article("hello pug")
  p This is my
  p Amazing article

編譯結果:

<div class="article">
  <div class="article-wrapper">
    <h3>Hello worle</h3>
    <p>NO content provided</p>
  </div>
</div>
<div class="article">
  <div class="article-wrapper">
    <h3>hello pug</h3>
    <p>This is my</p>
    <p>Amazing article</p>
  </div>
</div>

還有:

mixin link(href, name)
  a(class!=attributes.class href=href)= name
+link("/foo", "foo")(class="btn")

編譯結果:

<a class="btn" href="/foo">foo</a>

未知參數:

mixin list(id, ...items)
  ul(id=id)
    each item in items
      li= item
+list("my-list",1,2,3,4)

編譯結果:

<ul id="my-list">
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
</ul>

Extends 擴展

允許模板來擴展一個布局或父模板,它可以覆蓋某些預定義內容塊。

//- index.pug
extends layout.pug

block title
  title Article Title

block content
  h1 My Article
//- layout.pug
doctype html
html
  head
    block title
      title Default title
  body
    block content

編譯結果:

<!DOCTYPE html>
<html>

<head>
  <title>Article Title</title>
</head>

<body>
  <h1>My Article</h1>
</body>

</html>

Includes

允許將一個pug文件的內容插入到另一個文件。
在要插入的位置:include 文件地址

//- index.pug
doctype html
html
  include includes/head.pug
  body
    h1 My Site
    p Welcome to my super lame site.
    include includes/foot.pug
//- includes/head.pug
head
  title My Site
  script(src='/javascripts/jquery.js')
  script(src='/javascripts/app.js')
//- includes/foot.pug
footer#footer
  p Copyright (c) foobar

編譯結果:

<!DOCTYPE html>
<html>

<head>
  <title>My Site</title>
  <script src="/javascripts/jquery.js"></script>
  <script src="/javascripts/app.js"></script>
</head>

<body>
  <h1>My Site</h1>
  <p>Welcome to my super lame site.</p>
  <footer id="footer">
    <p>Copyright (c) foobar</p>
  </footer>
</body>

</html>

參考:https://pugjs.org/api/getting-started.html

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • Spring Boot 參考指南 介紹 轉載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,971評論 6 342
  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,767評論 18 399
  • 枝葉繁茂的花樹,醉人陽光。孤身一人漫步街頭,感覺像游離。城市里的聲音,漸漸從耳邊褪去,我注意到某個人的鞋子有兩種顏...
    凡卡閱讀 284評論 0 1
  • 落葉梧桐雨夜秋, 燈明小院綠籬休。 風來袖舞驚弦亂, 幸有香茗為我留。
    天水居士閱讀 270評論 0 7