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

Expand block template examples #13494

Merged
merged 2 commits into from
Jan 28, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions docs/designers-developers/developers/block-api/block-templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,51 @@ Planned additions:

Templates can be declared in JS or in PHP as an array of blockTypes (block name and optional attributes).

The first example in PHP creates a template for posts that includes an image block to start, you can add as many or as few blocks to your template as needed.


PHP example:

```php
<?php
function myguten_register_template() {
mkaz marked this conversation as resolved.
Show resolved Hide resolved
$post_type_object = get_post_type_object( 'post' );
$post_type_object->template = array(
array( 'core/image' ),
);
}
add_action( 'init', 'myguten_register_template' );
```

The following example in JavaScript creates a new block which uses [InnerBlocks](/packages/editor/src/components/inner-blocks) and templates to insert a set of blocks:

```js
const template = [
[ 'block/name', {} ], // [ blockName, attributes ]
const el = wp.element.createElement;
const { registerBlockType } = wp.blocks;
const { InnerBlocks } = wp.editor;

const BLOCKS_TEMPLATE = [
[ 'core/image', {} ],
[ 'core/paragraph', { placeholder: 'Image Details' }],
mkaz marked this conversation as resolved.
Show resolved Hide resolved
];
```

```php
'template' => array(
array( 'block/name' ),
),
registerBlockType( 'myguten/template', {
title: 'Myguten Template Block',
category: 'widgets',
edit: ( props ) => {
return el( InnerBlocks, {
template: BLOCKS_TEMPLATE,
templateLock: false
});
},
save: ( props ) => {
return el( InnerBlocks.Content, {} );
},
});
```

See the [Meta Block Tutorial](https://wordpress.org/gutenberg/handbook/designers-developers/developers/tutorials/metabox/meta-block-5-finishing/) for a full example of a template in use.
mkaz marked this conversation as resolved.
Show resolved Hide resolved

## Custom Post types

A custom post type can register its own template during registration:
Expand Down