简体   繁体   English

如何将Express.js变量传递给Jade / Pug非缓冲代码?

[英]How to pass Express.js variable to Jade/Pug unbuffered code?

I have an index.js with the following code: 我有一个带有以下代码的index.js:

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index',{
      "title":"Homepage",
      "navIndex":"reg",
      "navItems":[
         {"title":"Home", "link":"/" },
         {"title":"Register", "link":"/reg"}
      ]
  });
});

module.exports = router;

And I have index.pug: 我有index.pug:

doctype html
html
    head
        title Welcome!
        link(rel='stylesheet' href='css/materialize.min.css')
        link(rel='stylesheet' href='css/style.css')
        script(src='js/jquery-3.2.1.min.js')
        script(src='js/materialize.min.js')
    body
        block content
        nav
            div(class='nav-wrapper')
                a(href='#' class='brand-logo' class='right') Logo
                ul(id='nav-mobile' class='left hide-on-med-and-down')
                -
                    var navItems = #{navItems}

                    for(x = 0; x < navItems.length; x++)
                        li
                            a(href=navItems[x].link) navItems[x].title

For some reason I keep getting the "Unexpected block content" error. 由于某些原因,我一直收到“意外的块内容”错误。

I've tried adjusting my spacing, and I just can't seem to get it to work. 我尝试调整间距,但似乎无法正常使用。 I've looked at the documentation on unbuffered code, but I"m just not sure how to pass the navItems object from the index.js to the unbuffered code section of the pug file. I would assume you define a variable like I did ex. var navItems = #{navItems} . Is there any specific documentation on this? If not, what am I doing wrong, exactly? Thanks in advance. 我看过有关非缓冲代码的文档,但我只是不确定如何将navItems对象从index.js传递到pug文件的非缓冲代码部分。我假设您像在ex那样定义了一个变量var navItems = #{navItems} 。是否有任何具体的文档?如果没有,我到底在做什么错呢?预先感谢。

Edit: 编辑:

Updated index.pug is below. 更新的index.pug在下面。 But it outputs "navItems[x].title" verbatim, not the value of the variable. 但是它逐字输出“ navItems [x] .title”,而不是变量的值。

doctype html
html
    head
        title Welcome!
        link(rel='stylesheet' href='css/materialize.min.css')
        link(rel='stylesheet' href='css/style.css')
        script(src='js/jquery-3.2.1.min.js')
        script(src='js/materialize.min.js')
    body
        nav
            div(class='nav-wrapper')
                a(href='#' class='brand-logo' class='right') Logo
                ul(id='nav-mobile' class='left hide-on-small-only')
                    - for(x = 0; x < navItems.length; x++)
                    li
                        a(href=navItems[x].link) navItems[x].title

You have a block statement in your index.pug file, which indicates that this file is normally intended to be inherited by other templates, which will provide the content for your block content . index.pug文件中有一个block语句,该语句表明该文件通常旨在由其他模板继承,其他模板将为您的block content提供block content

I think removing block content statement from your index.pug should resolve your error. 我认为从index.pug删除block content语句应该可以解决您的错误。 Or you can provide a default content to your block content . 或者,您可以为block content提供默认block content

You can read more here about Template Inheritance in Pug . 您可以在此处阅读有关Pug中模板继承的更多信息

Regarding your updated code, I think the #{variableName} syntax is deprecated for attributes only (ie in Attribute Interpolation). 关于您更新的代码,我认为#{variableName}语法仅针对属性(即在属性插值中)已弃用。

When using as part of content, you can use either of following: 当用作内容的一部分时,可以使用以下任一方法:

a(href=navItems[x].link)= navItems[x].title
a(href=navItems[x].link) #{navItems[x].title}

Now, regarding the difference in indentation for the for loop, proper indentation is required if you are using Pug loops ie without a dash (-) at the start. 现在,关于for循环的缩进差异,如果您使用的是Pug循环,即在开始时没有破折号(-),则需要适当的缩进。

When you add a dash (-) at the start of a line, this line is treated as pure javascript, and then you need not indent the next line in most cases. 当您在一行的开头添加破折号(-)时,该行被视为纯JavaScript,因此在大多数情况下,您无需缩进下一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM