Nuxt.js: Loop over pages

Created on 3 Jan 2017  路  5Comments  路  Source: nuxt/nuxt.js

Apologies if this is documented somewhere. I am looking into whether I should replace my Jekyll site for Nuxt, is there a way to loop over pages?

E.g Jekyll has

{% for post in site.posts %}
    {{ post.title }}
{% endif %}

It would be nice if Nuxt had something similar, and if a page could expose meta data, e.g.

<script>
export default {
    meta: {
        title: 'Hello World'
    }
}
</script>

Thanks.

This question is available on Nuxt.js community (#c83)
question

Most helpful comment

Another way would be to use require.context() of Webpack to list the files of a specified directory and loop trough it :)

You can take a look at https://github.com/nuxt/nuxt.js/blob/master/lib/app/store.js to have an idea of how to use it.

If you add a markdown loader you may be also able to import them with webpack as well.

We want to develop nuxt-static with @alexchopin to create an easy alternative of Jekyll but with Nuxt.js

All 5 comments

Hi, thank for using Nuxt.

Actually there is no way to use it like Jekyll does with:

{% for post in site.posts %}
    {{ post.title }}
{% endif %}

However, you can easily split you content and your design by creating markdown files in a post directory for example:

pages/
  post/
     _slug.vue
  index.vue
post/
  welcome.md

then read this directory content to have the list of your post with nuxtServerInit function.
To parse markdown files content like Jekyll does, you can do it with front-matter.

I hope it will helps you :)

Another way would be to use require.context() of Webpack to list the files of a specified directory and loop trough it :)

You can take a look at https://github.com/nuxt/nuxt.js/blob/master/lib/app/store.js to have an idea of how to use it.

If you add a markdown loader you may be also able to import them with webpack as well.

We want to develop nuxt-static with @alexchopin to create an easy alternative of Jekyll but with Nuxt.js

@alexchopin

How can one read the post directory content within the nuxtServerInit function? Unless the post/ directory is exposed as static, the init func can't see that directory, right?

@Atinux Thanks for that tip. That's a smart solution. I want to replace my GitHub page / Jekyll site. I'd love a full alternative to Jekyll using Nuxt, but this should work for now. Thanks again!

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

surmon-china picture surmon-china  路  3Comments

mikekidder picture mikekidder  路  3Comments

bimohxh picture bimohxh  路  3Comments

danieloprado picture danieloprado  路  3Comments

nassimbenkirane picture nassimbenkirane  路  3Comments