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

Framework: Support nested templates #5141

Merged
merged 1 commit into from
Feb 20, 2018
Merged

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
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 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.

3 participants