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

Pass collection context to mustache #34

Merged
merged 9 commits into from
Aug 11, 2024
Merged

Pass collection context to mustache #34

merged 9 commits into from
Aug 11, 2024

Conversation

robinmetral
Copy link
Owner

@robinmetral robinmetral commented Aug 10, 2024

Closes #26

This is a pretty big change. Essentially, it:

  • introduces collections. Collections are defined in config and must match a directory name under the root pages directory
  • passes collection context to mustache. The context includes all posts (content and metadata) from these collections. It is passed to mustache as part of the view, which means it can be accessed in templates
  • lets users preprocess the context. This is useful because since mustache is logicless, the view needs to be build before passing it to the template

These should be enough to move a lot of logic out of custom build scripts, starting with the basic use case of iterating through a list of articles to render them on a page.

The context is not fed to mustache yet, this will be done in a follow-up commit.

This commit also includes cleanup around how paths are determined. There is one minor breaking change: the third attribute that was passed to a custom buildScript, the current page's slug, used to omit the trailing slash. It now includes it, for consistency.
BREAKING CHANGE: the mustache view changed from frontmatter `{ [key]: value }` to `{ page: { frontmatter: { [key]: value } }, context: { pages: Page[] } }`.

Where a template was referencing the frontmatter `title` via `{{title}}`, it now needs to be referenced as `{{page.frontmatter.title}}`.
Instead of containing all page metadata, context only contains pages that are part of collections. Collections are defined in config and must point to a directory that is a direct descendant of the pagesDir, e.g. `/pages/posts` 👉 `posts`.

Collections are based on the path (e.g. pages in `/pages/posts`). To make sure to only include collection pages and not collection indices (e.g. `/pages/posts/index.html`), we check if the page has a `published_date`. The hardcoding of the frontmatter date key is intentional: in a follow-up commit, we can automatically make the published_date a JavaScript Date, to avoid having to do this manually in a processContext script.

Inspired by Cobalt.
Copy link

cloudflare-workers-and-pages bot commented Aug 10, 2024

Deploying brut with  Cloudflare Pages  Cloudflare Pages

Latest commit: de19b9a
Status: ✅  Deploy successful!
Preview URL: https://32a28c27.brut.pages.dev
Branch Preview URL: https://context.brut.pages.dev

View logs

This is useful for testing sorting by date
This required another iteration through pages. Any better suggestion to solve this faster is welcome.
@robinmetral robinmetral changed the title Pass side-wide context to the templating engine Pass collection context to mustache Aug 11, 2024
@robinmetral robinmetral merged commit 2d3dadd into main Aug 11, 2024
2 checks passed
@robinmetral robinmetral deleted the context branch August 11, 2024 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose site-wide context to mustache
1 participant