htmltemplatespugtemplate-mixins

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


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?


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

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