Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot access the content of the "site" global variable in a template #3038

Closed
wsw70 opened this issue Feb 21, 2018 · 5 comments
Closed

Cannot access the content of the "site" global variable in a template #3038

wsw70 opened this issue Feb 21, 2018 · 5 comments

Comments

@wsw70
Copy link

wsw70 commented Feb 21, 2018

Environment Info

Node version(node -v): 8.9.4

Hexo and Plugin version(npm ls --depth 0):

For BUG

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.

@wsw70 wsw70 changed the title Cannot access the content of gloval variables in a template Cannot access the content of global variables in a template Feb 21, 2018
@wsw70
Copy link
Author

wsw70 commented Feb 22, 2018

When desperately analyzing the data provided by site, I ended up with the following workaround, but it looks very, very bad:

ul
    each s in site
        if s.data && s.length > 0
            each p in s.data
                if p.title
                    li= p.title

@wsw70
Copy link
Author

wsw70 commented Feb 22, 2018

I think this is a bug, or, if intended, documentation must be updated - the following works (please note the extra .data):

ul
    each i in site.posts.data
        li= i.title

@wsw70 wsw70 changed the title Cannot access the content of global variables in a template Cannot access the content of the "site" global variable in a template Feb 22, 2018
@stevenjoezhang
Copy link
Member

@stevenjoezhang
Copy link
Member

stevenjoezhang commented Mar 19, 2020

This is actually a documentation issue. site.posts is not iterable.
There is a discussion about it hexojs/site#1489
I'm closing this issue. Please join the discussion there :)

@noraj
Copy link
Contributor

noraj commented Nov 22, 2020

Calling site.posts.data seems not recommended (see #1567) you should rather do site.posts.toArray(). See http://hexojs.github.io/warehouse/Query.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants