-
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
Add render field to navigation block experiment #44665
Add render field to navigation block experiment #44665
Conversation
I just replicated the |
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 is not straightforward for developers with a JS background, but you can't declare functions in the render file because that file will be called once per block in the page, and functions in PHP are added to the global scope, so the second time it will fail with a "function redeclaration" problem.
You can use any of these approaches, but you'd most likely want to move those declarations to another file and use a require_once
.
Oh, you're totally right. It breaks if I add two
I wanted to quickly test this by moving the functions to a new
require_once(dirname(dirname(dirname(__DIR__))) . '/packages/block-library/src/navigation/helper.php');
I guess this would be solved using Well, and I didn't know how to name the new file 😄 . Is there any convention for this? |
That's a terrible DX 😄 I don't know how feasible it would be to use Webpack to inspect the
Maybe clean your Gutenberg repo (remove
I don't have any other ideas than these ones. @noisysocks suggested using namespaces, but I don't know how that would play out. Robert, can you explain that in more detail? Thanks 🙂 @ryanwelcher suggested using the For Gutenberg core, I think using |
They wouldn't help for this case. Namespaces would be helpful if you were having name conflicts with a method in a different block, e.g. if both Navigation and Post Comments defined a What you probably want here is to put all of the declarations (functions, classes) into a separate file which is imported using |
Thanks, Robert 🙂
|
I've just pushed a commit adding the |
I still think it's a big improvement over the |
@noisysocks, using a single template file that is included for every block instance is tricky when you embed functions. However, themes run into exactly the same issues and they developed good practices that address that. Shared utility functions are extracted to files that are included once by the theme like |
True I support we could rework |
Another side note 😅: I think something like this proposed API would work really well for blocks like Navigation where there is a huge amount of (fragile) HTML concatenation. |
I agree. A component system for PHP similar to that one is something we should research at some point 🙂 |
Yes, that might help to have something similar to JSX on PHP side 👍 |
What?
I wanted to test how the new
render
property could work in the Experiment: Replicate Navigation block using directives.Why?
In that experiment, we are changing a lot of the HTML, and doing it in a string is sometimes complicated. Using the
render
property could ease the workflow.How?
I mainly moved the code of the
render_block_core_navigation
function outside of it, to store the variables. And moved thesprintf
of the navigation block to HTML.