Node version(node -v): 8.9.4
Hexo and Plugin version(npm ls --depth 0):
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
I want to use global variable in a Pug template, specifically list the titles of all my posts (there are two of them):
p first line of the page
ul
each i, j in site.posts
- console.log(i, j, site.posts[i])
li= po
p last line of the page
This code outputs the following HTML file
<p>first line of the page</p>
<ul>
<li></li>
<li></li>
</ul>
<p>last line of the page</p>
There are two empty <li></li>, which suggests that the iteration over the posts was successful.
The console output with debug enabled. Please note the console.log() output of mine, which shows that two indexes of the array were iterated over
16:53:18.040 DEBUG Hexo version: 3.5.0
16:53:18.043 DEBUG Working directory: D:\MegaSync\dev-perso\blog-vuetify\blog\
16:53:18.151 DEBUG Config loaded: D:\MegaSync\dev-perso\blog-vuetify\blog\_config.yml
16:53:18.164 DEBUG Plugin loaded: hexo-generator-archive
16:53:18.167 DEBUG Plugin loaded: hexo-generator-category
16:53:18.170 DEBUG Plugin loaded: hexo-generator-tag
16:53:18.173 DEBUG Plugin loaded: hexo-generator-index
16:53:18.453 DEBUG Plugin loaded: hexo-render-pug
16:53:18.459 DEBUG Plugin loaded: hexo-renderer-ejs
16:53:18.528 DEBUG Plugin loaded: hexo-server
16:53:18.531 DEBUG Plugin loaded: hexo-renderer-stylus
16:53:18.536 DEBUG Plugin loaded: hexo-renderer-marked
16:53:18.566 DEBUG Loading database.
16:53:18.581 INFO Start processing
16:53:18.592 DEBUG Processed: _posts/hello-world.1.md
16:53:18.596 DEBUG Processed: _posts/hello-world.md
16:53:18.602 DEBUG Processed: layout/footer.ejs
16:53:18.603 DEBUG Processed: layout/header.ejs
16:53:18.607 DEBUG Processed: layout/mypost.ejs
16:53:18.608 DEBUG Processed: layout/nav.ejs
16:53:18.610 DEBUG Processed: layout/index.pug <--- the template file mentioned above
16:53:18.619 DEBUG Generator: page
16:53:18.620 DEBUG Generator: post
16:53:18.621 DEBUG Generator: archive
16:53:18.623 DEBUG Generator: category
16:53:18.623 DEBUG Generator: tag
16:53:18.624 DEBUG Generator: index
16:53:18.625 DEBUG Generator: asset
16:53:18.629 INFO Files loaded in 62 ms
16:53:18.634 DEBUG Rendering index: 2018/02/20/hello-world.1/index.html
16:53:18.640 DEBUG Rendering mypost: 2018/02/20/hello-world/index.html
16:53:18.647 DEBUG Rendering index: archives/index.html
16:53:18.649 DEBUG Rendering index: archives/2018/index.html
16:53:18.651 DEBUG Rendering index: archives/2018/02/index.html
16:53:18.653 DEBUG Rendering index: tags/tech/index.html
16:53:18.655 DEBUG Rendering index: index.html*undefined 0 undefined
undefined 1 undefined
undefined 0 undefined
undefined 1 undefined
undefined 0 undefined
undefined 1 undefined
undefined 0 undefined <--- my console.log() output
undefined 1 undefined
undefined 0 undefined
undefined 1 undefined
undefined 0 undefined
undefined 1 undefined
16:53:18.731 INFO 0 files generated in 100 ms
16:53:18.737 DEBUG Database saved
I had the same problem with ESJ as the template engine.
When desperately analyzing the data provided by site, I ended up with the following workaround, but it looks very, very bad:
each s in site
if s.data && s.length > 0
each p in s.data
if p.title
li= p.title
I think this is a bug, or, if intended, documentation must be updated - the following works (please note the extra .data):
each i in site.posts.data
li= i.title
This is actually a documentation issue. site.posts is not iterable.
There is a discussion about it https://github.com/hexojs/site/issues/1489
I'm closing this issue. Please join the discussion there :)
Calling site.posts.data seems not recommended (see #1567) you should rather do site.posts.toArray(). See http://hexojs.github.io/warehouse/Query.html
Most helpful comment
I think this is a bug, or, if intended, documentation must be updated - the following works (please note the extra
.data):