-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Blocks: Add new render
field to block.json
#42430
Conversation
fd9dcef
to
f1f1b37
Compare
This is very interesting 🙂 I'm trying to figure out what would be the DX of adding functions without facing the redeclaration error:
Is there any other option? |
IMO, there really shouldn't be any function declarations in the template file at all. This file can be conceptually compared to files that would be used with This is definitely a case of making sure there is proper documentation and best practices around this feature but I think that any functionality this block requires is better defined in the |
Yeah, I guess that makes sense from a WordPress point of view. Sorry, I was thinking from the perspective of devs without much WP/PHP background that want to componentize their markup logic, so they just start adding functions to those entry points. And probably thinking of a future where just adding a bunch of |
Could use a |
The example included in the Blocks ACF plugin doesn't contain any function and class declarations:
So that explanation makes perfect sense here. It's also similar to what you can observe in templates for themes, for example: |
The name is a bit weird to me. What would be the relationship between a |
@mtias IMO as a PHP-first developer, It is also closer in naming to |
The semantics of |
Interesting observation. I didn't consider that at all. I guess the |
|
That's an interesting take. We have only {
"view": [ "file:./template.php", "file:./view.js", "file:./view.css" ]
} It would be less straightforward when passing script or style handles, but we could always run some detection whether this handle is for a style, a script, or both. I would love to hear what @luisherranz thinks about all these ideas in the context of the block hydration experiments. |
Categorizing the PHP rendering as Today, technically speaking, the PHP rendering is the dynamic equivalent of the Anyway, if I had to vote, I'd use the |
It's only processed on the server, so we shouldn't be worried about renaming that later and even keeping it backward-compatible. I'm fine with using
It's more nuanced. We have more places these days where |
f1f1b37
to
0a4f367
Compare
I updated this branch upon the feedback received. I included documentation changes necessary for the new Before merging this PR, I will have to remove changes from the Archive block, as noted in the description. Everything else should be ready for the final feedback. |
renderTemplate
field to block.json
render
field to block.json
be9a8d8
to
e114dd9
Compare
I removed the refactoring applied to the Archive block that was presenting the idea. I think this PR received enough feedback so let's make the final decision whether it's good enough to get included in WordPress 6.1. |
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 think this API is very valuable. In the scaffolding we use for custom block development we already have a similar abstraction which we could drop in favor of the core approach if this were to land.
Also, thank you @gziolo for writing such great documentation for this feature! :) It is really appreciated <3
Co-authored-by: Ari Stathopoulos <aristath@gmail.com>
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.
This looks and works great, thank you @gziolo!
Here's a caveat for a follow-up PR: The Gutenberg plugin build process does not bundle the render.php
files in the build directory, which means the render scripts cannot be used with core blocks yet.
We don't necessarily need to immediately refactor any of the existing core blocks to use Line 47 in 1d778aa
|
I landed the current version, but I'd like to double-check what do you think about making it possible to pass also the callback name in the |
Hello, Allowing multiple values for this render attribute could be possible ? and use an get_template_part like function ? This will allow to redefine Dynamic blocs, even core ones, within themes. This will allow to have a template hierarchy like rendering process for Dynamic blocs. Nicolas, |
Not at the moment. Similar to how
Interesting idea. You can get close to that with WP hook. See |
New `render` field in `block.json` file that accepts a string value. It allows to pass a path to the PHP file that is going to be used to render the block on the server. Related PR in Gutenberg: WordPress/gutenberg#42430. Props spacedmonkey, luisherranz, welcher, noisysocks, matveb, fabiankaegy, aristath, zieladam. Fixes #53148. git-svn-id: https://develop.svn.wordpress.org/trunk@54132 602fd350-edb4-49c9-b593-d223f7449a82
I backported this new API to WordPress core with https://core.trac.wordpress.org/changeset/54132. |
New `render` field in `block.json` file that accepts a string value. It allows to pass a path to the PHP file that is going to be used to render the block on the server. Related PR in Gutenberg: WordPress/gutenberg#42430. Props spacedmonkey, luisherranz, welcher, noisysocks, matveb, fabiankaegy, aristath, zieladam. Fixes #53148. Built from https://develop.svn.wordpress.org/trunk@54132 git-svn-id: http://core.svn.wordpress.org/trunk@53691 1a063a9b-81f0-0310-95a4-ce76da25c4cd
New `render` field in `block.json` file that accepts a string value. It allows to pass a path to the PHP file that is going to be used to render the block on the server. Related PR in Gutenberg: WordPress/gutenberg#42430. Props spacedmonkey, luisherranz, welcher, noisysocks, matveb, fabiankaegy, aristath, zieladam. Fixes #53148. Built from https://develop.svn.wordpress.org/trunk@54132 git-svn-id: https://core.svn.wordpress.org/trunk@53691 1a063a9b-81f0-0310-95a4-ce76da25c4cd
New `render` field in `block.json` file that accepts a string value. It allows to pass a path to the PHP file that is going to be used to render the block on the server. Related PR in Gutenberg: WordPress/gutenberg#42430. Props spacedmonkey, luisherranz, welcher, noisysocks, matveb, fabiankaegy, aristath, zieladam. Fixes #53148. Built from https://develop.svn.wordpress.org/trunk@54132
New `render` field in `block.json` file that accepts a string value. It allows to pass a path to the PHP file that is going to be used to render the block on the server. Related PR in Gutenberg: WordPress/gutenberg#42430. Props spacedmonkey, luisherranz, welcher, noisysocks, matveb, fabiankaegy, aristath, zieladam. Fixes #53148. git-svn-id: https://develop.svn.wordpress.org/trunk@54132 602fd350-edb4-49c9-b593-d223f7449a82
What?
A technical prototype for Editor: Consider adding
render
property for block types in WordPress Trac. I will add tests and documentation only when people find this proposal useful.Related to the proposal #41289 from @ryanwelcher to add
--is-dynamic
flag to@wordpress/create-block
.Why?
See comment from @spacedmonkey #13693 (comment).
There is also another thread started by @spacedmonkey in #13693 (comment) which expands on this topic.
How?
New
render
field inblock.json
file that accepts a string value.The path to the file should be prefixed with
file:
and be relative to theblock.json
file in the build (target) folder. It's exactly the same pattern as used with scripts and styles inblock.json
.PHP file that renders the template for the block can use 3 variables that are the same as
render_callback
takes as params:$attributes
(array
) - block attributes.$content
(string
) - block default content.$block
(WP_Block
) - block instance.Important note: I refactored the Archive block to use the newRemoved from PR.render
field inblock.json
only for testing purposes. We rather won't do it as using function declarations is problematic with this approach because template files sometimes need to load several times for a given block type. When that happens, then we encounter a function redeclaration error.Testing Instructions
Ensure that the temporarily refactored Archive blocks still work as before.
To test with custom blocks you need to add
render
field in theblock.json
file.Example:
src/block.json
src/render.php