funkyeah
funkyeah

Reputation: 3194

Meteor js: use #each to iterate over and render templates in multiple html files for a blog

I know meteor isn't exactly meant for this but I've already implemented the website so no turning back now.

What I have is a series of blog posts. Right now they are all in one large template. I would like to split them out into a html file for each and then iterate over those html files when rendering the blog layout template.

How could this be done? I would also be open to other solutions/approaches that achieve the same end goal (allows me to better organize these self-contained static html pieces)

Upvotes: 0

Views: 932

Answers (1)

Tarang
Tarang

Reputation: 75975

You could do something like this

{{#each post}}
    {{#if isBlogPost}}
        {{> blogPostTemplate}}
    {{else}}
        {{> someOtherTemplate}}
    {{/if
{{/each}}

Then you can have your templates in different files e.g

file1.html

<template name="blogPostTemplate">
    {{title}}
</template>

file2.html

<template name="someOtherTemplate">
    Other Template, Maybe for ads?
</template>

If you need a html file for each post

Template.blog.getPost = function(templateName) {
    return Meteor.render(Template[templateName]);
}

Then you can use something like this, which lets you use a custom template name passed off from a js object or collection

{{getPost 'blogPostTemplate'}}

Upvotes: 2

Related Questions