2017-07-27 40 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()也必须用逗号分隔。