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

Communicating between createPage and the template, how? #11774

Closed
kuworking opened this issue Feb 14, 2019 · 1 comment
Closed

Communicating between createPage and the template, how? #11774

kuworking opened this issue Feb 14, 2019 · 1 comment
Labels
type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@kuworking
Copy link

When creating pages with templates, the way the docs guide us (AFAIU) here is that by creating a page with context, we'll be able to retrieve this context when on the template file.

But the key is here:

... the gatsby-source-filesystem plugin ships with a function for creating them ...
Add your new slugs directly onto the MarkdownRemark nodes. Any data you add to nodes is available to **query** later with GraphQL.

so that anything that you add here (from docs):

createPage({
  path: node.fields.slug,
  component: path.resolve(`./src/templates/blog-post.js`),
  context: {
    slug: node.fields.slug,
  },
})

you will be able to select it when inside the template by querying it:

export const query = graphql`
  query($slug: String!) {
    markdownRemark(fields: { slug: { eq: $slug } }) {
      html
      frontmatter {
        title
      }
    }
  }
`

But, if you don't use the plugin, then whatever you add to the context will be there, but not in the schema (?), so you will not be able to query for your specific node by using $slug

Then, the question is, how is this done? How can I send to the template the content I want it to format? Because when I'm in the template I only have access to graphql that shows me all the existing nodes, but no obvious way to use the specific node I am processing at that time

@gatsbot gatsbot bot added the type: question or discussion Issue discussing or asking a question about Gatsby label Feb 14, 2019
@kuworking
Copy link
Author

I think I have it, you can (docs) directly extract the context in the template

export default ({ pageContext }) => {

and from there you have the info you've passed before in gatbsy-node.js (like pageContext.html)

        createPage({
          path: `${post.name}`,
          component: path.resolve(`./src/templates/blog-post.js`),
          context: {
            name: `${post.name}`,
            html: `${file.contents}`,
          },
        })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
Development

No branches or pull requests

1 participant