【问题标题】:give context from route to pug mixin给出从路由到 pug mixin 的上下文
【发布时间】:2018-03-30 07:47:30
【问题描述】:

我正在尝试通过快速路由向仅包含一个 mixin 的 pug 文件提供上下文。但是当我尝试渲染该文件时,它什么也不做。

This is my express route

app.post('/comment', (req, res) => {
      const test = {
          text: 'working',
      };
      res.render('comment-section', test);
});

这是我的 pug 文件 mixin

mixin comment()
  div.comment-content
    div.row
        div.col-lg-1
          a(href="#")
              img(src="img/default_profile.jpg").comment-profile-img
        div.col-lg-10
          p.user-comment
              | Comment //- <--- Here i would like to use the test object this way {#text}
    div
      textarea(name="post_content" placeholder="Comment something" rows="1").comment-textarea.post-comment-message

如何让 pug mixin 从路由中获取上下文(测试对象)并使用它。 提前致谢!

【问题讨论】:

    标签: express pug viewengine


    【解决方案1】:

    不是 pug 的专业人士,但我认为您应该提供要明确使用的参数:

    mixin comment(text)
      div.comment-content
        div.row
          div.col-lg-1
            a(href="#")
              img(src="img/default_profile.jpg").comment-profile-img
          div.col-lg-10
            p.user-comment= text //- or #{text}, assuming {#text} was a typo
      div
        textarea(name="post_content" placeholder="Comment something" rows="1").comment-textarea.post-comment-message
    

    现在可以如下使用了

    - var text = '<- var should already exist when locally provided'
    #comment-section
      +comment(text)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-02
      • 1970-01-01
      • 2021-08-10
      • 1970-01-01
      • 2018-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多