-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Docs: Fundamentals of Block Development - Static or Dynamic rendering of a block #57250
Docs: Fundamentals of Block Development - Static or Dynamic rendering of a block #57250
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left two comments in here :)
Also I would ideally want to see some code examples alongside these.
Also is this really new documentation that we don't already have multiple times over? I feel like this something that used to exist at least 🤔
I'm a fan of how this developer blog post from earlier this year covered the topic: https://developer.wordpress.org/news/2023/02/27/static-vs-dynamic-blocks-whats-the-difference/
A block can define dynamic rendering can be defined in two ways: | ||
- The `register_block_type()` function includes a `render_callback` argument. | ||
- A `render` property is added to `block.json`, whose value points to a separate PHP file (usually named `render.php`). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this is very confusing. This explains how to use dynamic rendering without explaining first what dynamic rendering even is / letting the reader know that there are two different kinds of rendering
## Blocks with Static Rendering | ||
|
||
Blocks with static rendering **generate the HMTL for the frontend at "update-time" (when it's saved)**: | ||
- Blocks with static render use their markup representation in the DB to return the markup used in the frontend for the block | ||
- The `save()` function writes the block’s markup inside the block indicators (HTML comments). | ||
- Only the markup inside the block indicators is returned as markup to be rendered for the block in the frontend. | ||
- For blocks with static render, the markup returned to the frontend is defined/set when the page is saved. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically this is not entirely correct. Yes static blocks save their markup to the database and return said markup on the frontend. However both core and extenders can and do modify that markup before it gets rendered on the frontend via the render_callback
or render_block
hook.
An example of this is the core cover block which is a static block that does some PHP magic if the "use featured image" setting to dynamically inject the featured image.
Same with the Image block. Yes it saves markup to the DB. But when the image is served through the media library the sourceset
generation etc is done on the server.
The List block is a static block that dynamically gets it's wrapping class name (wp-block-list
) injected into the blocks markup on the server.
My point here is not that this should all be documented here. Instead I worry that the current explanation makes one feel too "safe" that the markup generated in the save
function in JS is output 100% as is without modification. Which is not true.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My point here is not that this should all be documented here. Instead I worry that the current explanation makes one feel too "safe" that the markup generated in the save function in JS is output 100% as is without modification. Which is not true.
Thanks for this explanation. Based on your explanations I agree the current text may be misleading.
I'll work on a different text proposal to take these cases into account.
Thanks @fabiankaegy for your comments.
Yes! I was planning to reference some examples from https://github.com/WordPress/block-development-examples
The main purpose of this Fundamentals of Block Development is to surface some content that is considered essential for block development (at least from a practical point of view) Regarding this static vs dynamic rendering, one of the ideas that we'd like to reinforce is that there is no strictly "static blocks" or "dynamic block" but more blocks with static rendering or blocks with dynamic rendering. Based on feedback received, we believe this new approach will make it easier to understand the real possibilities of rendering a block. |
🤦 sorry, I somehow missed that since I somehow got pinged for a review |
@fabiankaegy I have finally opened the PR for review with the first version for this page. One of the purposes of the Fundamentals of Block Development is to surface the knowledge for the most relevant topics in Block Development. This page takes some content from How-to Guides > Blocks > Creating dynamic blocks with the intention of removing this part from that page it once this PR has been approved and merged |
Flaky tests detected in 9f629b7. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7299803797
|
Co-authored-by: Nick Diego <[email protected]>
Co-authored-by: Nick Diego <[email protected]>
Co-authored-by: Nick Diego <[email protected]>
Co-authored-by: Nick Diego <[email protected]>
Co-authored-by: Nick Diego <[email protected]>
Co-authored-by: Nick Diego <[email protected]>
Co-authored-by: Nick Diego <[email protected]>
Co-authored-by: Nick Diego <[email protected]>
Co-authored-by: Nick Diego <[email protected]>
Co-authored-by: Nick Diego <[email protected]>
Co-authored-by: Nick Diego <[email protected]>
…' of github.com:WordPress/gutenberg into fundamentals-block-development/static-dynamic-rendering
@justintadlock thanks a lot for your feedback. |
Static or Dynamic rendering of a block
This PR fixes #56274