Skip to content

Commit

Permalink
Expand block template examples (#13494)
Browse files Browse the repository at this point in the history
* Expand block template examples

Adds examples for PHP and JS for using templates.

Fixes #8251

* Consolidate examples and cleanup

Update function names to be consistent across example.
  • Loading branch information
mkaz authored and youknowriad committed Mar 6, 2019
1 parent 57e6f74 commit a28cc93
Showing 1 changed file with 46 additions and 22 deletions.
68 changes: 46 additions & 22 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,50 @@ 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 myplugin_register_template() {
$post_type_object = get_post_type_object( 'post' );
$post_type_object->template = array(
array( 'core/image' ),
);
}
add_action( 'init', 'myplugin_register_template' );
```

The following example in JavaScript creates a new block using [InnerBlocks](/packages/editor/src/components/inner-blocks) and templates, when inserted creates a set of blocks based off the template.

```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' } ],
];
```

```php
'template' => array(
array( 'block/name' ),
),
registerBlockType( 'myplugin/template', {
title: 'My 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](/docs/designers-developers/developers/tutorials/metabox/meta-block-5-finishing.md) for a full example of a template in use.

## Custom Post types

A custom post type can register its own template during registration:
Expand Down Expand Up @@ -61,20 +93,7 @@ add_action( 'init', 'myplugin_register_book_post_type' );
Sometimes the intention might be to lock the template on the UI so that the blocks presented cannot be manipulated. This is achieved with a `template_lock` property.

```php
'template_lock' => 'all', // or 'insert' to allow moving
```

*Options:*

- `all` — prevents all operations. It is not possible to insert new blocks, move existing blocks, or delete blocks.
- `insert` — prevents inserting or removing blocks, but allows moving existing blocks.

## Existing Post Types

It is also possible to assign a template to an existing post type like "posts" and "pages":

```php
function my_add_template_to_posts() {
function myplugin_register_template() {
$post_type_object = get_post_type_object( 'post' );
$post_type_object->template = array(
array( 'core/paragraph', array(
Expand All @@ -83,9 +102,14 @@ function my_add_template_to_posts() {
);
$post_type_object->template_lock = 'all';
}
add_action( 'init', 'my_add_template_to_posts' );
add_action( 'init', 'myplugin_register_template' );
```

*Options:*

- `all` — prevents all operations. It is not possible to insert new blocks, move existing blocks, or delete blocks.
- `insert` — prevents inserting or removing blocks, but allows moving existing blocks.

## Nested Templates

Container blocks like the columns blocks also support templates. This is achieved by assigning a nested template to the block.
Expand Down

0 comments on commit a28cc93

Please sign in to comment.