Skip to content

Conversation

@youknowriad
Copy link
Contributor

@youknowriad youknowriad commented Feb 19, 2018

This PR adds support for nested templates. For example, a CPT could use a columns block in its templates and assign a template for this nested block.

This uses the third argument of the array defining a block in a template as a way to define a sub-template.

This doesn't address nested locking yet. This will be worked on separately.

Testing instructions

  • Define a block type with nested templates like this:
function register_book_type() {
	$args = array(
		'public' => true,
		'label'  => 'Books',
		'show_in_rest' => true,
		'template' => array(
			array( 'core/image' ),
			array( 'core/paragraph', array(
				'placeholder' => 'Add a book description',
			) ),
			array( 'core/quote' ),
			array( 'core/columns', array(), array(
				array( 'core/image', array( 'layout' => 'column-1' ) ),
				array( 'core/paragraph', array(
					'placeholder' => 'Add a inner paragraph',
					'layout' => 'column-2' 
				) ),
			) )
		),
	);
	register_post_type( 'book', $args );
}
add_action( 'init', 'register_book_type' );
  • Check that when inserting a new book, the template is initialized with a prefilled columns block.

@youknowriad youknowriad added [Feature] Extensibility The ability to extend blocks or the editing experience [Feature] Templates API Related to API powering block template functionality in the Site Editor labels Feb 19, 2018
@youknowriad youknowriad self-assigned this Feb 19, 2018
@youknowriad youknowriad requested review from aduth and mtias February 19, 2018 11:30
return block;
} );
const createBlocksFromTemplate = ( template ) => {
return map( template, ( [ name, attributes, subTemplate ] ) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should subTemplate be innerBlocks?

Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍

Minor point: I'd prefer consistency on "inner blocks" naming than introducing "sub" as a concept, e.g. innerBlocksTemplate.

@youknowriad youknowriad force-pushed the add/nested-templates-support branch from c5caf6b to 76d2ec0 Compare February 20, 2018 07:34
@youknowriad youknowriad merged commit f6c3a74 into master Feb 20, 2018
@youknowriad youknowriad deleted the add/nested-templates-support branch February 20, 2018 07:50
@mtias
Copy link
Member

mtias commented Feb 22, 2018

This is something we should mention on the templates doc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Extensibility The ability to extend blocks or the editing experience [Feature] Templates API Related to API powering block template functionality in the Site Editor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants