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

Conditionally loading block templates classic themes #45341

Closed
abaicus opened this issue Oct 27, 2022 · 10 comments
Closed

Conditionally loading block templates classic themes #45341

abaicus opened this issue Oct 27, 2022 · 10 comments
Labels
[Feature] Themes Questions or issues with incorporating or styling blocks in a theme. [Focus] Blocks Adoption For issues that directly impact the ability to adopt features of Gutenberg. [Type] Enhancement A suggestion for improvement.

Comments

@abaicus
Copy link
Contributor

abaicus commented Oct 27, 2022

What problem does this address?

It is currently impossible to specify which templates should be loaded inside classic themes.

There are two theme supports to enable block templates and template parts:

add_theme_support( 'block-template-parts' );
add_theme_support( 'block-templates' );

A classic theme might implement a complex archive layout with many customization features but still want to allow the possibility to use a template for the archive based on a customizer setting.

I believe that this extends to block-based themes as well. If you want to define templates for CPTs (e.g., WooCommerce Single Product), these will be loaded automatically, regardless of the plugin’s state. When it comes to classic themes, authors would probably want to allow the user to pick which templates he intends to override with block templates.

I’ve seen that WooCommerce has managed to load templates from the plugin in a custom class, but that feels a bit cumbersome.

What is your proposed solution?

Allowing an arguments array with which templates to be loaded for both theme supports could be a good solution:

add_theme_support( 'block-templates',
	array(
		'index'      => get_template_directory() . '/includes/fse-templates/index.html',
		'front-page' => get_template_directory() . '/includes/fse-templates/front-page.html',
	)
);
@annezazu annezazu added [Type] Enhancement A suggestion for improvement. [Feature] Themes Questions or issues with incorporating or styling blocks in a theme. [Focus] Blocks Adoption For issues that directly impact the ability to adopt features of Gutenberg. labels Oct 27, 2022
@annezazu
Copy link
Contributor

@WordPress/block-themers tagging in this crew as I think this will be of interest!

@jffng
Copy link
Contributor

jffng commented Oct 27, 2022

👋 So I understand, the suggestion is to enable classic themes to specify a different file path for a block-based template to override in the hierarchy?

Apologies if I'm missing something, but why not just enable the theme support and supply the block based version of the template in the location that Core will look for, e.g. block-templates/front-page.html?

@abaicus
Copy link
Contributor Author

abaicus commented Oct 28, 2022

@jffng

That would make sense as well. Just allowing a configurable array of which templates are being loaded would be good enough, I think.

@jffng
Copy link
Contributor

jffng commented Nov 1, 2022

Why is a configurable array necessary? Currently if a block template is provided and the theme support is added + enabled, the block template will override the classic one accordingly in the hierarchy. For example: WordPress/wordpress-develop#3545 — archive pages are rendered with the block template and all other pages with the classic template.

@abaicus
Copy link
Contributor Author

abaicus commented Nov 2, 2022

@jffng

It just occurred to me that filtering out the templates you don't want to load using the get_block_templates filter would solve this.

That would solve the issue. I'm not sure why I didn't think about this before. 😶‍🌫️

Thank you for being so helpful!

@jffng
Copy link
Contributor

jffng commented Nov 2, 2022

Happy to help and try to understand different use cases!

@annezazu
Copy link
Contributor

annezazu commented Nov 3, 2022

@abaicus is it okay if we close this out considering? Perhaps it's worth updating this handbook page around this specific use case since it seems to have resonated too: https://developer.wordpress.org/themes/block-themes/converting-a-classic-theme-to-a-block-theme/

@abaicus
Copy link
Contributor Author

abaicus commented Nov 3, 2022

@annezazu Yes, I think we can close the issue. It would be great if it could be mentioned in the handbook. 👍🏻

Thank you for all the help!

@carolinan
Copy link
Contributor

I think we may need a little bit more information to help us update the documentation. @abaicus Would you be able to share an example where you are using the filter?

@abaicus
Copy link
Contributor Author

abaicus commented Nov 4, 2022

@carolinan I've added this functionality in this commit: Codeinwp/raft@a3d9919.

Please let me know if you think any additional details are needed! 👍🏻

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Themes Questions or issues with incorporating or styling blocks in a theme. [Focus] Blocks Adoption For issues that directly impact the ability to adopt features of Gutenberg. [Type] Enhancement A suggestion for improvement.
5 participants