例如,假设我有一个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."})
这会有用吗?还有更好的方法吗?
答案 0 :(得分:2)
是的,它可以工作,但你的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."])