Question

For example, say I have a mixin to create blog posts:

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

Used like so:

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

Results in:

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

Which works well, but let's say I don't know how many paragraphs are in the "post content" part of the post, I only know that there will be one or more. So, for example, the content of a post might be:

**Title**
My awesome blog post

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

This is a new paragraph about my awesome blog post.

Would something like this do the trick?

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

Called like so:

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

Would this work? Is there a better way?

Was it helpful?

Solution

Yes it would work but your mixin logic is not quite right, and you need to pass the content paragraphs as an array of strings not an object as you have in your example.

Mixin Changes

  • remove for keyword
  • set p= paragraph, not content which is the array

With these changes your mixin should look something like this

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

Then just remember to call the mixin with an array of strings

+blogPost("Post Title", ["This is my awesome blog post.","This is a new paragraph about my awesome blog post."])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top