繁体   English   中英

如何将多段内容作为参数传递给Jade mixin

[英]How to pass multiple-paragraph content to a Jade mixin as an argument

例如,假设我有一个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."})

这会有用吗? 有没有更好的办法?

是的,它会起作用,但你的mixin逻辑不太正确,你需要将内容段落作为字符串数组传递,而不是像示例中那样对象。

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."])

暂无
暂无

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

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