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

Template Editing: Templates do not recognize theme template parts without a theme attribute #36124

Open
kjellr opened this issue Nov 1, 2021 · 7 comments
Labels
[Block] Template Part Affects the Template Parts Block [Feature] Template Editing Mode Related to the template editor available in the Block Editor [Feature] Themes Questions or issues with incorporating or styling blocks in a theme. [Type] Bug An existing feature does not function as intended

Comments

@kjellr
Copy link
Contributor

kjellr commented Nov 1, 2021

A theme attribute is not required for template parts to render in the site editor, however it is required in the template editor.

The fallback hierarchy here should be the same, so that the Template editor knows to look for templates in the theme when there's no theme slug provided.

To reproduce:

The following uses Twenty Twenty-Two as an example, but this should be the case with any block theme that uses a template part in its templates.

  1. Use the function from Add an API to define the default template used for the template mode #32771 to map the default Template to one of your theme's existing block templates (one that includes a template part).
/**
 * Add a default block template.
 */
function twentytwentytwo_default_block_template() {
	add_filter( 'block_editor_settings_all',
		function( $settings ) {
			$settings['defaultBlockTemplate'] = file_get_contents( get_theme_file_path( 'block-templates/single.html' ) );
			return $settings;
		}
	);
}
add_action( 'after_setup_theme', 'twentytwentytwo_default_block_template', 0 );
  1. Open a post or page in the editor, and select "New" under the Template panel:

Screen Shot 2021-11-01 at 12 52 41 PM

  1. Note that if your theme uses a theme slug to point to its template part, the template part will appear. Otherwise it will show a "Template not found" error.

Screenshots:

With theme attribute Without theme attribute
Screen Shot 2021-11-01 at 12 43 32 PM Screen Shot 2021-11-01 at 12 42 01 PM
@kjellr kjellr added [Type] Bug An existing feature does not function as intended [Feature] Themes Questions or issues with incorporating or styling blocks in a theme. [Feature] Template Editing Mode Related to the template editor available in the Block Editor labels Nov 1, 2021
@carolinan carolinan added the [Block] Template Part Affects the Template Parts Block label Nov 2, 2021
@Mamaduka
Copy link
Member

Mamaduka commented Nov 2, 2021

Here's a workaround for now:

/**
 * Add a default block template.
 */
function twentytwentytwo_default_block_template( $settings ) {
	$template_content                 = file_get_contents( get_theme_file_path( 'block-templates/single.html' ) );
	$settings['defaultBlockTemplate'] = _gutenberg_inject_theme_attribute_in_content( $template_content );

	return $settings;
}
add_filter( 'block_editor_settings_all', 'twentytwentytwo_default_block_template' );

@kjellr
Copy link
Contributor Author

kjellr commented Nov 12, 2021

@jffng / @carolinan what do you think about that workaround above? If that is generally ok then we can use it for Twenty Twenty-Two. Otherwise I think we need to have this issue prioritized for 5.9.

@jffng
Copy link
Contributor

jffng commented Nov 12, 2021

It works, though it looks like _gutenberg_inject_theme_attribute_in_content was renamed to _inject_theme_attribute_in_block_template_content in case you want to test it out.

what do you think about that workaround above?

I don't like the idea of adding this to the theme, since the utility function comes from this file which states:

Temporary compatibility shims for features present in Gutenberg.
This file should be removed when WordPress 5.9.0 becomes the lowest
supported version by this plugin.

@Mamaduka
Copy link
Member

You should use _inject_theme_attribute_in_block_template_content in theme. But I agree it’s private utility and third parties shouldn’t depend on it.

@kjellr
Copy link
Contributor Author

kjellr commented Nov 12, 2021

Ok, thanks for weighing in. @noisysocks — what do you think about prioritizing this for 5.9? It's preventing block themes from using theme-supplied template parts in the template editor.

@noisysocks
Copy link
Member

I'll add it to the board as a general bug to fix (not a blocker).

@Mamaduka
Copy link
Member

Mamaduka commented Jan 5, 2022

I'm removing this issue from WP 5.9 project board since RC1 was released last night.

We can add the defaultTemplate option to the theme.json and handle theme attribute injection internally. Probably task for the next major release since we have a workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Template Part Affects the Template Parts Block [Feature] Template Editing Mode Related to the template editor available in the Block Editor [Feature] Themes Questions or issues with incorporating or styling blocks in a theme. [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

No branches or pull requests

5 participants