-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
FSE: Pre-populate template with fallback upon creation #42007
FSE: Pre-populate template with fallback upon creation #42007
Conversation
Thanks for exploring this Bernie and for all your efforts to solve this problem! From my initial tests this seems to work really well! For context this is part of the DM I sent to Bernie:
Even though I suggested the modification of |
TBH I’d rather avoid introducing a new endpoint. The functional overlap with the current one would be fairly large, so I’d be wary of fragmenting the REST API surface for templates too much. Since both would allow reading the template content for a given slug, it might be confusing for developers to know what each endpoint is for, let alone to reason about where any changes should go to.
It’s not complete yet, though: It doesn’t yet support the full template hierarchy (https://wphierarchy.com/); among other things, the fallback from frontend to home template is missing (which is currently implemented elsewhere https://github.com/WordPress/wordpress-develop/blob/ae9cff730f54f2401ce3271477b1ba63fed81f83/src/wp-includes/block-template.php#L335-L365 but would be good to absorb into the logic here). IMO, extending
One reason why I think that using the fallback to create a new template (vs. starting blank) makes sense is the following:
|
@ntsekouras and I had a call earlier today. While neither of us managed to convince the other as to whether modifying the existing endpoint’s behavior or adding a new endpoint is the better solution 😬 (see above discussion), we decided it made sense to open this PR for review already, in order to solicit others’ feedback. Some notes: The full template hierarchy isn’t covered by this PR yet, but we agreed it would be easy enough to extend it iteratively (and with proper test coverage) to account for e.g. the frontpage -> home fallback, and more. Further note that as of now, all templates eventually fall back to the index template. While this is consistent with the template hierarchy (as implemented on the frontend), it was brought up that this can be counter-intuitive in some cases, e.g. for 404 or single templates. I’m happy to add logic for these special cases. My question would be what should be the fallback there? Just start blank? Finally, as an alternative to adding a new endpoint, I suggested adding a field to indicate using fallback mode during template creation (akin to this). Curious to hear y’all’s thoughts! |
I personally agree, I prefer less "magical" behavior in REST API endpoints.
Yes, I don't think the client would be the one to follow the template hierarchy, IMO that would still be done server side. So I would see a REST API endpoint that could be used to resolve an arbitrary template hierarchy. Then the client would grab the content for the matched post, and pass it to the existing create endpoint. Those are my initial thoughts, but I don't think I have a full understanding of what is in play here. So let me know if I'm missing something. |
I tested the flows outlined in the OP plus a few more and everything seems to be working as expected! :)
For single templates perhaps core can provide a minimal fallback that consists of something like:
Obviously this should only be used after looping through all available templates and finding no singular template. For 404 we can do something similar:
What do you think? |
@jameskoster I think that makes sense! In practice, I'd probably implement it by including those templates as files with GB (and -- once backported -- with Core). This would mark a bit of a departure from how themes used to work in absence of a given template, but that's probably okay. |
Thanks @TimothyBJacobs for your thoughts!
It's maybe relevant to point out that template resolution can mean two different things:
Personally, I don't like the idea too much to have:
It's two more network roundtrips than with this PR, and I'm not sure it's worth having a (Sidenote: Maybe it'd make eventually sense to have a template resolution endpoint that emulates the actual routing as done by WordPress on the frontend, i.e. given a route, returns the template that's in charge of rendering that. E.g. Well, that probably sounded a bit defensive when I really just wanted to clarify my reasoning for this PR 😅 |
I think one endpoint for the above two is enough. Making just an extra request for the content and keeping the basic |
But that would mean that we'd have two endpoints that accept a template slug and return the template's content -- only that one would use the fallback lookup whereas the other one wouldn't (unless I'm misreading you). But that's the kind of duplication of functionality that I thought would confuse people (and likely be problematic with regard to maintenance -- i.e. if a developer wants to change something and does it to one endpoint but not the other)... |
Thanks for your detailed thoughts @ockham. I think we can avoid the pitfalls you describe. As mentioned by @ntsekouras we can consolidate the first two requests via a couple of different methods.
|
Closing as obsolete, now that #42520 has been merged. |
What?
Alternative to #41848, based on some ideas shared by @ntsekouras in DM.
Purports to fix #36648, as an alternative approach to #37054 and #41848. For more background, see #37054 (comment)
(More description to come)
Why?
See e.g. #37258 (comment) and #37258 (comment).
How?
By using the "next best" template per template hierarchy resolution to pre-populate a newly created template's content.
Testing Instructions
This testing routine will verify a number of things:
First, we create a "Single Post" template and verify that it comes pre-populated with the contents of the theme-supplied "Single" template:
Second, we create a "Single Post" template for a specific post and verify that it comes pre-populated with the generic "Single Post" template that we just created: This is still buggy, reload every time you go back to the template list
Ideally, give it some more smoke testing -- whatever you can think of makes sense: delete the template again; find a way to test it on the frontend; etc.