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

Add dynamic block documentation #4067

Merged
merged 6 commits into from
Dec 19, 2017
Merged

Add dynamic block documentation #4067

merged 6 commits into from
Dec 19, 2017

Conversation

atimmer
Copy link
Member

@atimmer atimmer commented Dec 18, 2017

I wanted to onboard one of our developers onto writing a dynamic Gutenberg block, but I couldn't find the documentation. So I've added the documentation from @youknowriad's blog to the handbook. Taken from https://riad.blog/2017/10/16/one-thousand-and-one-way-to-extend-gutenberg-today/#dynamic-block. I've rewritten some sentences to better fit the handbook.

@@ -0,0 +1,77 @@
It is also possible to create dynamic blocks. These are blocks that can change their content even if the post is not saved. One example from WordPress itself is the latest posts block. This block will update everywhere it is used whena new post is published.
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we add a "title" for the page.

"It is also possible ..." suggest that we read something before. Is this the case? or should we drop "also"

"whena" should be "when a"

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

Some small comments, but thanks for adding this. I also had some people asking about it.


The following code example shows how to create the latest post block dynamic block.

```js
Copy link
Member

Choose a reason for hiding this comment

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

Per consistency with other docs, should we include ES5 and ESNext varieties here?

registerBlockType = wp.blocks.registerBlockType,
withAPIData = wp.components.withAPIData;

registerBlockType( 'riad/latest-post', {
Copy link
Member

Choose a reason for hiding this comment

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

Per consistency with other docs, should we use a more generic namespace here, since without context of blog it's sourced from, unclear what Riad's name is doing here 😄 Other places we're using my-plugin.

<?php
// block.php

function riad_render_block_latest_post( $attribites ) {
Copy link
Member

Choose a reason for hiding this comment

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

More Riad 😄 (and below in register_block_type)

Copy link
Contributor

Choose a reason for hiding this comment

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

Don't be jealous :P


* The edit function still shows a representation of the block in the editor's context (this could be very different from the rendered version, it's up to the block's author)
* The save function just returns null because the rendering is performed server-side.
* The server-side rendering is a function taking the block attributes as an argument and returning the markup (quite similar to shortcodes)
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to be clearer about how attributes are also defined on the server for this to work correctly? Though in this case there are no attributes for this block.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know, I've copied these points from Riad's post. Could we merge this and iterate on it further?

<?php
// block.php

function my_plugin_render_block_latest_post( $attribites ) {
Copy link
Member

Choose a reason for hiding this comment

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

Typo: $attribites -> $attributes

Should we omit this if it's an unused variable?

@aduth aduth merged commit 64ac510 into WordPress:master Dec 19, 2017
@atimmer atimmer deleted the add/docs-dynamic-blocks branch December 20, 2017 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants