Using Patterns in Templates
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
)
This would be very useful, any plans to implement this?
Would also be very interested in this.
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!
Fantastic! I'm using it for post type block template. It's solve also https://github.com/WordPress/gutenberg/issues/33379
Thanks
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!
Hey @troytempleman - just checking in on this - did you ever find a way to get it working with Template Lock?
@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.
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 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.
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.
I also would like to see this done. Seems potentially related to / could use the same solution as #49005
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.
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
templateLockattribute 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.
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.