wpengine / faustjs

Faust.js™ - The Headless WordPress Framework
https://faustjs.org
Other
1.44k stars 134 forks source link

Possible Templates - Check WP Template in Seed Query #1191

Closed JerresonJ closed 1 year ago

JerresonJ commented 1 year ago

I am trying to specify the same template name for several pages in the WP Backend. So that I can create a single wp-template file in the front-end that is used for those specific pages. The wp-template file calls a general layout each page uses and goes from there.

I may be misunderstanding something here but shouldn't the template assigned to the page be a 'possible template' on the front end?

Seed Query returns template data but this data seems to be ignored as 'possible templates'

    template: {
      __typename: 'Template_MyTemplate',
      templateName: 'My Template'
    },

So shouldn't I see something like page-my-template as a possible template to use here?

Is there another way to specify a single wp-template file for a use like this or should we just skip this hierarchy here and use file based routes?

Leaning towards file based routing but I figured the idea of using the template designated as a possible template wouldn't hurt.

theodesp commented 1 year ago

Hey @Jonathan-Garber. If you want to change the Template Resolution ordering you can do that with a plugin. Take a look at the example Blueprint code here:

https://github.com/wpengine/atlas-blueprint-portfolio/blob/336efb3df95f3ec0654373fe3ebe52ca8f48cc12/plugins/ProjectTemplatePlugin.js#L1-L12

The plugin adds a filter to the possibleTemplatesList that is getting called just after we resolve the templates from the seedQuery. In that case we inspect the seedQuery object as documented here, and check if a specific template exists. If yes we can prepend a new template name that we use for routing. This is used to resolve the Project template for example.

In your case, you could create another plugin that checks if the templateName exists and prepend a new template name for that route.

Let me know if this would cover your use-case.

JerresonJ commented 1 year ago

Can this be included in the core of Faust... as opposed to a a plugin? I haven't tested it as a plugin yet but it seems to me the entire idea of seed querying is to know what template to use. Well we assigned the template in WP already so it should look for that matching template by default.

theodesp commented 1 year ago

@Jonathan-Garber Thanks I will add a ticket to review since the templateName can accept any type of string, we need to check how to encode this as well.

JEverhart383 commented 1 year ago

I was looking back through open issues and found someone else in Discord asking for this same functionality in a round-about way. We got it to work with a plugin, but it could be worth considering since custom page templates are technically are a part of the template hierarchy & block themes will make custom templates much easier to create: https://discord.com/channels/836253505944813629/1087807356206862346

theodesp commented 1 year ago

https://github.com/wpengine/faustjs/pull/1401

blakewilson commented 1 year ago

Hey @Jonathan-Garber,

This is currently released in our nightly release, and should be out in an official Faust release soon.

If you'd like to test it/provide feedback, you can install the nightly release by running:

npm install @faustwp/core@canary

in your project.

From there, in the possible templates list, you should see the actual WordPress template as well.

theodesp commented 1 year ago

Closing this one since its merged.