JSRocksHQ / harmonic

The next static site generator
http://harmonicjs.com/
MIT License
282 stars 26 forks source link

Unable to use page meta-data inside pages #161

Open jaydson opened 9 years ago

jaydson commented 9 years ago

I just figure out users are unable to use pages meta-data inside in the pages context. Example: I want to list all pages I have in a template (imagine a menu). This can be done easily in the index.html template, or even in partials (in the index context):

{% for page in pages %}
        <li class="meni__item">
            <a href="{{ page.href }}">{{ page.title | capitalize }}</a>
        </li>
{% endfor %}

Why this is possible? Looking at Harmonic's code (src/bin/parser.js) in the function generateIndex:

const indexContent = indexTemplateNJ.render({
  posts,
  config,
  pages: pagesMetadata
});

As you can see, we're binding the pages meta-data.

But, unfortunately we can't use the same logic for pages, because this pages meta-data aren't being binding. Look (src/bin/parser.js) in the function generatePages:

pageHTMLFile = pageTplNJ.render({
  page: _page,
  config: config
});

We just have the current page data, and not all pages.

jaydson commented 8 years ago

This issue was solved by #166?

UltCombo commented 8 years ago

I believe #166 was related to a pages i18n issue. This issue is about exposing all pages' metadata to every generated page, right? I believe that is not working yet.

Note that generatePages and generatePosts have been merged into the generateFiles method, so our code base is a bit different now. I believe we can just expose all the posts and pages metadata from the language that are compiling to every generated post and page in the same language, here.

UltCombo commented 8 years ago

On a second thought, the generateFiles method currently only has access to one given file type (post or page)'s metadata at a time, so while it is possible to expose all pages' metadata to every generated page, we wouldn't be able to expose all pages' metadata to generated posts—I think that would be useful too, don't you think?

Note that, currently, as generateFiles only access one given file type at a time, we are able to compile posts and pages independently in parallel. We would have to change the generation logic in order to allow generated posts to access pages' metadata.

jaydson commented 8 years ago

Yeah, I was wondering about that. I think all data should be exposed.

Use cases:

UltCombo commented 8 years ago

:+1: We just need to get it done, then. In the future, we can think about normalizing the template data across all kinds of templates, that is, making the template data structure as similar as possible across different templates in order to reduce the cognitive overhead.