【问题标题】:How to pass multiple-paragraph content to a Jade mixin as an argument如何将多段内容作为参数传递给 Jade mixin
【发布时间】:2014-01-23 15:00:38
【问题描述】:

例如,假设我有一个 mixin 来创建博客文章:

mixin blogPost(title, content)
    article
        h1= title
        p= content

这样使用:

+blogPost("Post Title","Post Content")

结果:

<article>
    <h1>Post Title</h1>
    <p>Post Content</p>
</article>

效果很好,但假设我不知道帖子的“帖子内容”部分有多少段,我只知道会有一个或多个。因此,例如,帖子的内容可能是:

**Title**
My awesome blog post

**Post Content** 
This is my awesome blog post.

This is a new paragraph about my awesome blog post.

这样的东西可以解决问题吗?

mixin blogPost(title, content)
article
    h1= title
    - for each paragraph in content
        p= content

这样称呼:

+blogPost("Post Title", {"This is my awesome blog post.","This is a new paragraph about my awesome blog post."})

这行得通吗?有没有更好的办法?

【问题讨论】:

    标签: html templates pug template-mixins


    【解决方案1】:

    是的,它可以工作,但你的 mixin 逻辑不太正确,你需要将内容段落作为字符串数组而不是你在示例中的对象传递。

    混音变化

    • 删除for关键字
    • 设置p= paragraph,而不是数组content

    通过这些更改,您的 mixin 应该看起来像这样

    mixin blogPost(title, content)
    article
        h1= title
        - each paragraph in content
            p= paragraph
    

    然后记得用一个字符串数组调用 mixin

    +blogPost("Post Title", ["This is my awesome blog post.","This is a new paragraph about my awesome blog post."])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-11
      • 1970-01-01
      • 1970-01-01
      • 2012-07-18
      • 2015-10-27
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多