如何在pug.js模板中正确拼写Django的语句?

时间:2017-07-27 09:16:14

标签: python django django-templates preprocessor pugjs

当我尝试编译时:

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上找不到,尽管这里的语法是{{3与我的相似:

for a in b
  = a

2 个答案:

答案 0 :(得分:1)

此模板中存在多个错误。 这是正确的版本:

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 }}

请注意,pug语法中没有结束标记,这就是我喜欢它的原因! :) 还要注意 title = ,而不是在它们之间留一个空格,这就是打破它。内部()内的参数也必须用逗号分隔。

答案 1 :(得分:0)

所以这只是一个消息,但只需通过添加|字符确保所有django标签都以纯文本形式读取

我一直在尝试用简单的哈巴狗原型设计前端,然后使用输出作为我的模板,这似乎有效

我还没试过pypugjs但似乎原来的回购已经被那个从pyjade中分叉出来的人删除了。