Code patterns: Initial experiment. #173
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What
This past week, I’ve been experimenting with different approaches for creating patterns. My requirements were:
The Experiment
I decided to go with the PHP route—it’s simpler and handles loops and conditionals easily. But there are caveats:
We don’t have post context available when registering a pattern.
ACF creates custom blocks as entire patterns and treats each field as an “editable part.” We want to stick with standard blocks inside standard patterns.
I am avoiding parsing the PHP file content. If I was to do that, I'd need to parse the HTML, process the PHP, and then merge everything. That way I could:
Instead, I went for a “just use PHP functions” approach. For example:
This outputs:
This code:
Outputs:
While this works, the developer experience still isn’t as smooth as classic themes.
We can now use the full power of block bindings with our new source, scf/experimental-field. That means fields can be edited directly in the editor UI.
Better yet, those inputs can live in the post sidebar (instead of being buried in metaboxes).
patterns_demo.mov