Template Part: Better handling of "missing" parts
What problem does this address?
When a template part block references a part that is missing, you see the following error on the canvas:
It's okay for theme developers, but when an end-user encounters this it's not super helpful. It's not something folks are likely to encounter now, but as we enrich the template creation flow, and the pattern directory expands to include template-specific patterns, it might start to crop up more often.
It might be nice to explore some actionable items we could add to the message, and in some circumstances avoid showing the message at all.
What is your proposed solution?
A couple of ideas off the top of my head
- Update the view above to include a "Choose" button which opens a modal containing options (local and from the pattern / theme directory)
- When creating a template (either by manually pasting in the blocks, or via the built-in flows) if a missing part is referenced, automatically swap it out for a local equivalent of the same semantic area, and display a confirmational Snackbar.
Update the view above to include a "Choose" button
Something along this is what I'm thinking as well. Currently you have to delete the block entirely and add it again to get to the placeholder state.
Maybe something like this:
I don't love "Choose" as the action; not super clear.
has been deleted
Perhaps "could not be found" instead? That would be more universal, thinking about situations where you paste a template from one site into another.
Should there be a 'Start blank' or 'Create new' option, to cover situations in which there are no pre-existing template parts or relevant patterns?
Perhaps "could not be found" instead? That would be more universal, thinking about situations where you paste a template from one site into another.
True, good point.
Should there be a 'Start blank' or 'Create new' option, to cover situations in which there are no pre-existing template parts or relevant patterns?
I don't feel that would be a likely course of action—more likely to remove probably—though I'm fine either way.
Perhaps 'Start blank' is only an option when no existing template parts / relevant patterns can be found? Otherwise the whole thing feels a bit pointless with only a 'Remove' action.
It might be good to list the different scenarios to ensure we have a design / actions that works for all? Off the top of my head we need to consider:
Specified template part not found, and:
- There are existing template parts of the same type from which the user can choose
- There are existing template parts of the same type from which the user can choose, and there are relevant patterns as well
- There are relevant patterns, but no existing template parts of the same type
- There are no existing template parts of the same type, or any relevant patterns
In the last two cases, the 'Choose another template part...' sentence in the design above wouldn't make sense, so that might need to be dynamic.
Perhaps 'Start blank' is only an option when no existing template parts / relevant patterns can be found? Otherwise the whole thing feels a bit pointless with only a 'Remove' action.
We'd still have "Choose" though, right? I think the two routes are "Yes, I deleted that template part, why is this still here" or "yikes, I don't know what happened to that template part, let me pick another one."
We'd still have "Choose" though, right?
I guess this is an edge case now that core bundles header / footer patterns, but what would happen when you click "Choose" if there are no template parts or patterns?
If there are none, then yea "Choose" shouldn't be an option.
Right, and in that case don't you think it's reasonable to offer a shortcut to create a new template part?
I don't have a strong feeling btw, just thinking: if there's intent to insert a template part, and that template part cannot be found, then it's fairly likely folks would want to create a new one in its place.
I've been banging my head lately on why it was failing...
I've been naming my files using double dashes -- as separator, so that I can use single dash - as an space in the file name and -- as a separator (just my convention, since I can't use path based routing).
If template part file name has two dashes it will show the Template part has been deleted or is unavailable: header--index error.
But only after selecting a design and refreshing the page (either by pressing F5 or throught the browser UI).
You can still navigate through the editor and choose and save the template with other designs, until you refresh. It will then fail.
I've found WordPress doesn't really like double dashes in the template part file names. So I had to change my file names so they use single dashes:
Then it works fine. Just my two cents on a problem that I recently had with my own custom theme. I also tested this using TwentyTwentyFive and WordPress 6.9 with Gutenberg as the only plugin.
I would expect that since there's no problem using double dashes for pattern file names, shouldn't be a problem to use double dashes for template part file names either.
I guess that If we could just use paths, we could avoid these weird naming conventions, organizing our files in folders, avoiding naming colissions etc. We can use subfolders in the patterns folder, but I would say the slug doesn't understand file paths.
@jameskoster when you get a moment to look, what's an action item on this one?
Perhaps the simplest solution would be to show a dialog to alert the user that a template part(s) could not be found, and the block as been removed, (and obviously perform the removal on confirm). Showing a broken block on the canvas is just unnecessary noise that can linger and create a sense of brokenness.
This would need to occur any time the editor tries to reference a non-existent template part, for instance pasting blocks from one site to another.