gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Using Patterns in Templates

Open uamv opened this issue 6 years ago • 14 comments

I've begun tinkering with the experimental block patterns API and expected that I might be able to register patterns and then call upon those registered patterns alongside other registered blocks when defining the template of a CPT.

It'd be great to simply build a template using existing patterns. If this is not already possible in some way, I propose it be considered how to make such templating available.

// From the registration of a CPT
'template' => array(
  array( 'my-plugin/my-pattern' ), // including a pattern
  array( 'core/paragraph' ) // including a block
)

uamv avatar Apr 23 '20 15:04 uamv

This would be very useful, any plans to implement this?

nachos5 avatar Sep 10 '20 09:09 nachos5

Would also be very interested in this.

aandrewjoyce avatar Nov 30 '20 14:11 aandrewjoyce

I've closed #36323 as a duplicate of this.

That issue was encountered during a triage session in slack (https://wordpress.slack.com/archives/C02QB2JS7/p1636435134063000).

There was a suggestion there to use the newly introduced pattern block in the template (see https://github.com/WordPress/gutenberg/pull/33217).

That block has a slug attribute that can be used to reference a pattern - https://github.com/WordPress/gutenberg/blob/trunk/packages/block-library/src/pattern/block.json#L13.

@uamv @nachos5 @aandrewjoyce ethanclevenger91 It'd be good to get your feedback on whether this option will work for you. Thanks!

talldan avatar Nov 09 '21 05:11 talldan

Fantastic! I'm using it for post type block template. It's solve also https://github.com/WordPress/gutenberg/issues/33379

Thanks

illycz avatar Dec 07 '21 13:12 illycz

The pattern block works as a CPT 'template' for me too, but unfortunately I can't lock it the same way as I could before with an array version of the template.

As soon as I add 'template_lock' => 'all' to the CPT, the template layout disappears and reverts to default.

Maybe I'm doing something wrong though. Does anyone have a working solution to locking a pattern?

Thanks!

troytempleman avatar Dec 30 '21 17:12 troytempleman

Hey @troytempleman - just checking in on this - did you ever find a way to get it working with Template Lock?

jordesign avatar Aug 18 '23 02:08 jordesign

@jordesign no sorry, I’ve since gone down a block plugin rabbit hole that I’m still in, lol. Will probably be revisiting block themes/templates soon though.

troytempleman avatar Aug 18 '23 11:08 troytempleman

Given that much has changed in the past couple of years - I'll close this issue as not current. But if you do run into problems again in the future please feel free to open a new issue with updated details.

jordesign avatar Aug 21 '23 00:08 jordesign

@jordesign But although much changed there is still no complete solution when it comes to the template lock. If e.g. you set template_lock to 'contentOnly' when using the pattern block in the template, to work around the issue, the pattern blocks won't be loaded. Because inserting is locked and that is how the pattern block injects the blocks that are part of the pattern.

I would like to request this to be reopened in the hope some dev will be able to come up with a solution that handles template lock as expected.

bvlgn avatar Sep 18 '23 15:09 bvlgn

Absolutely want this feature! It almost works right now, but you have to toggle the code editor on/off in order for the pattern to render.

ouw-jvt avatar Oct 24 '23 19:10 ouw-jvt

I also would like to see this done. Seems potentially related to / could use the same solution as #49005

mikeybinns avatar May 15 '24 17:05 mikeybinns

But although much changed there is still no complete solution when it comes to the template lock.

I think this is possible, you can wrap the pattern contents in a group block (so that the group is the top level block inside the pattern) and use the templateLock attribute of the group block.

talldan avatar May 16 '24 02:05 talldan

I think this is possible, you can wrap the pattern contents in a group block (so that the group is the top level block inside the pattern) and use the templateLock attribute of the group block.

Unfortunately, this only locks the pattern and does not prevent modifications to the template (e.g. adding new blocks) which was the initial intent. I've had to fall back to writing the template content in PHP instead of using a pattern.

slpy9 avatar Dec 21 '24 07:12 slpy9

Unfortunately, this only locks the pattern and does not prevent modifications to the template (e.g. adding new blocks) which was the initial intent. I've had to fall back to writing the template content in PHP instead of using a pattern.

If it's a problem, then I think it needs a new issue. This one is meandering away from what the OP requested.

talldan avatar Dec 23 '24 06:12 talldan