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

Rethink the way how page templates work to support multiple templates per post type #19907

Open
Mahjouba91 opened this issue Jan 27, 2020 · 5 comments
Labels
[Feature] Templates API Related to API powering block template functionality in the Site Editor [Type] Enhancement A suggestion for improvement.

Comments

@Mahjouba91
Copy link
Contributor

Mahjouba91 commented Jan 27, 2020

Is your feature request related to a problem? Please describe.
I'm frustrated about how templates currently work in Gutenberg.

You can implement a template through PHP like this:

function register_editor_templates() {
	$post_type_object = get_post_type_object( 'post' );
	if ( null !== $post_type_object ) {
		$post_type_object->template = array(
			array(
				'core/paragraph',
				array(
					'placeholder' => __( 'Write an introduction...' ),
				),
			),
			array( 'core/more' ),
			array(
				'core/paragraph',
				array(
					'placeholder' => __( 'Write more details...' ),
				),
			),
		);
	}
}
add_action( 'init', 'register_editor_templates' );

The problem is that you can only assign one template to a post type, and that this template will be automatically loaded at every new post creation.

Then we have the JS way by creating a new block. So it's nice because you can select any template you want for you post type but it's still not ideal. When adding a template through a block, it just loads all blocks of this template in the current post, even if you have already like 5 blocks in your post, it will just add it anywhere you want. so if you make a mistake and choose the wrong template for example, it's tricky, you will end up with a lot of blocks mixed in your content.

Describe the solution you'd like
I would like a solution similar to what we had previously with the classic editor. That means, to have a select box in order to choose any template we want so it would change all blocks of the current post.

If the current template param from a post type could be an array of templates and not juste an array of one template, it would do the trick for the API side, and tou will have to change the behaviour in frontend I guess.

I have also raised another issue that would help to have more granular templates #19874

Describe alternatives you've considered
Basically, the two official implementations all have drawbacks, so I can not implement the way I need it for the creators.

@jorgefilipecosta jorgefilipecosta added [Type] Enhancement A suggestion for improvement. [Feature] Templates API Related to API powering block template functionality in the Site Editor labels Feb 7, 2020
@pwkip
Copy link
Contributor

pwkip commented Mar 30, 2020

I would love to (help) implement this. Just wondering what the API should look like.

I guess it could look as simple as this:

$template1 = [
	[
		'core/image'
	],
	[
		'core/paragraph',
		[ 'placeholder' => 'Add a root-level paragraph' ]
	],
];

$template2 = [
	[
		'core/image'
	] 
];

$templates = [
	[
		'templateName' => __('My first template'),
		'template' => $template1
	],
	[
		'templateName' => __('My second template'),
		'template' => $template2
	],
];

And then the template property could take the $templates array like this

$post_type_object->template = $templates;

In the sidebar it could look like this:
Select Block Template

Thoughts?

@Mahjouba91
Copy link
Contributor Author

Hey, thanks for your comment.

Yes, that would be the best option I think

@LukaszJaro
Copy link

Does the new block pattern feature solve this? Create a pattern and add it to any number of pages?

I have yet to try it but it may be a solution to this.

@skorasaurus
Copy link
Member

Does the new block pattern feature solve this? Create a pattern and add it to any number of pages?

I have yet to try it but it may be a solution to this.

You can add the block pattern to as many or as few pages as you would like. The unfortunate part is that, as of now, you cannot add any block patterns (not even one) to PHP templates - this is being tracked in #21830

I think you'll be able to add them in full site editing templates; but I can't confirm that.

@maniflames
Copy link

I'm currently working on a theme that needs two different templates for a custom post type and I would love it if I could just register two. I've tried to see how the mentioned issues relate to this but they all seem to lean towards inserting patterns instead. Is the support for multiple templates on the roadmap somewhere? I'd love to still do some logic on the back-end depending on the a template a user has selected if that makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Templates API Related to API powering block template functionality in the Site Editor [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

6 participants