2017-07-27 64 views
0

當我嘗試編譯此:如何在pug.js模板中正確拼寫Django的語句?

doctype html 
html(lang="en") 
    head 
    meta(http-equiv = "Content-Type " content ="text/html ;charset=utf-8") 
    title = category.name 
    body 
    h1 Сьпiс таварау 
    h2 Катэгорыi: 
    ul 
     for cat in cats 
     li: a(href='/goods/{{cat.id}}/') {{cat.name}} 
     endfor 
    h2 Тавары 
    table 
     tr 
     th Назва 
     th Есьць у наяунасьцi 
     for good in goods 
     tr 
      td a(href = '/goods/good/{{good.id}}/'){{good.name}} 
     endfor    

我得到這個錯誤:

Error: index.pug:10:7 
    8|  h2 Катэгорыi: 
    9|  ul 
    > 10|  {% for cat in cats %} 
--------------^ 
    11|   li: a(href='/goods/{{cat.id}}/') {{cat.name}} 
    12|  endfor 
    13|  h2 Тавары 

unexpected text "{% fo" 
    at makeError (/home/ivan/Documents/node-v6.11.0-linux-x64/lib/node_modules/pug-cli/node_modules/pug-error/index.js:32:13) 
    at Lexer.error (/home/ivan/Documents/node-v6.11.0-linux-x64/lib/node_modules/pug-cli/node_modules/pug-lexer/index.js:58:15) 
    at Lexer.fail (/home/ivan/Documents/node-v6.11.0-linux-x64/lib/node_modules/pug-cli/node_modules/pug-lexer/index.js:1304:10) 
    at Lexer.advance (/home/ivan/Documents/node-v6.11.0-linux-x64/lib/node_modules/pug-cli/node_modules/pug-lexer/index.js:1364:15) 
    at Lexer.callLexerFunction (/home/ivan/Documents/node-v6.11.0-linux-x64/lib/node_modules/pug-cli/node_modules/pug-lexer/index.js:1319:23) 
    at Lexer.getTokens (/home/ivan/Documents/node-v6.11.0-linux-x64/lib/node_modules/pug-cli/node_modules/pug-lexer/index.js:1375:12) 
    at lex (/home/ivan/Documents/node-v6.11.0-linux-x64/lib/node_modules/pug-cli/node_modules/pug-lexer/index.js:12:42) 
    at Object.lex (/home/ivan/Documents/node-v6.11.0-linux-x64/lib/node_modules/pug-cli/node_modules/pug/lib/index.js:99:27) 
    at Function.loadString [as string] (/home/ivan/Documents/node-v6.11.0-linux-x64/lib/node_modules/pug-cli/node_modules/pug-load/index.js:44:24) 
    at compileBody (/home/ivan/Documents/node-v6.11.0-linux-x64/lib/node_modules/pug-cli/node_modules/pug/lib/index.js:86:18) 

起初我以爲這for cat in cats通常編譯成 {% for cat in cats %},喜歡它與if聲明的做法,但它看起來像pug.js需要一些更特殊的語法,我在pugjs.org上找不到,儘管這裏Pugjs official website上的語法與我的類似:

for a in b 
    = a 
+1

這沒有意義。帕格似乎是一個輸出HTML的Javascript預處理器。你打算如何使用Django模板? Django有什麼機會呈現模板? –

+0

@DanielRoseman還有一個特殊的插件。 https://github.com/matannoam/pypugjs Pug只是一個html預處理語言,就像sass和更少的css預處理語言,咖啡和打字稿是js預處理語言。 – NiHao92

+0

@DanielRoseman基本上,我懶得把所有關閉標籤的html,這是你不需要在帕格。即使無法通過Django呈現Pug模板,但使用pug編寫並使用pug編譯爲html .pug仍然可以節省時間 – NiHao92

回答

0

所以這只是一個sugestion只是確保所有的Django通過增加|字符

我一直在嘗試用簡單的哈巴狗原型前端過,然後使用輸出標籤的讀取爲純文本爲我的模板,這似乎工作

我還沒有嘗試pypugjs,但它似乎原來的回購已被刪除的人,原先從pyjade分出它。

+0

我重新啓動了pypugjs:https://github.com/kakulukia/pypugjs 並且爲了玩耍有了它,使用這個項目模板來設置並準備好:https://github.com/kakulukia/django-default-project –

0

此模板中有多個錯誤。 這裏是正確的版本:

doctype html 
html(lang="en") 
    head 
    meta(http-equiv="Content-Type", content="text/html ;charset=utf-8") 
    title= category.name 
    body 
    h1 Сьпiс таварау 
    h2 Катэгорыi: 
    ul 
     for cat in cats 
     li: a(href='/goods/{{ cat.id }}/') {{ cat.name }} 
    h2 Тавары 
    table 
     tr 
     th Назва 
     th Есьць у наяунасьцi 
     for good in goods 
     tr 
      td 
      a(href='/goods/good/{{good.id }}/') {{ good.name }} 

注意到有在哈巴狗語法沒有結束標籤而這就是爲什麼我喜歡它! :) 另請注意title =而不是有一個空間之間,這是打破它。參數inside()也必須用逗號分隔。