Skip to content

Commit

Permalink
[Create Block] Adding documentation for the transformer property (#60445
Browse files Browse the repository at this point in the history
)

* Adding documentation for the transformer property

* Remove unneeded line of code in example.

Co-authored-by: ryanwelcher <welcher@git.wordpress.org>
Co-authored-by: justintadlock <greenshady@git.wordpress.org>
Co-authored-by: gziolo <gziolo@git.wordpress.org>
  • Loading branch information
4 people authored and pull[bot] committed Jun 19, 2024
1 parent db7cd7a commit 3a24898
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/create-block/docs/external-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,32 @@ The following configurable variables are used with the template files. Template
- `style` (default: `'file:./style-index.css'`) – a frontend and editor style definition.
- `render` (no default) – a path to the PHP file used when rendering the block type on the server before presenting on the front end.
- `customBlockJSON` (no default) - allows definition of additional properties for the generated block.json file.
- `transformer` (default: `( view ) => view` ) - a function that receives all variables generated by the create-block tool and returns an object of values. This function provides the ability to modify existing values and add new variables.

#### `transformer` examples

This examples adds a generated value to the slug variable.

```js
transformer: ( view ) => {
const hex = getRandomHexCode();
return {
...view,
slug: `${ view.slug }-${ hex }`,
};
},
```

This example creates a new custom variable that can be used in the associated mustache templates as `{{customVariable}}`

```js
transformer: ( view ) => {
return {
...view,
customVariable: `Custom Value`,
};
},
```

### `variants`

Expand Down

0 comments on commit 3a24898

Please sign in to comment.