Use innerBlocks in a gutenberg dynamic Block ?
-
Hi everyone,
I am wondering about the right way to handle dynamic datas in a block requiring innerblocks.
I recently encountered an issue while trying to migrate dynamic post title to static core/heading. Part of the problematic happened because I was both using a dynamic block behaviour (render.php rendering php dynamic values such as post title), but also using a static save function to save innerBlocks.
Block.json{ ... "render": "file:./render.php", ... }
save functionsave: props => { return <InnerBlocks.Content /> }
Render.php
<section <?php echo get_block_wrapper_attributes(['class' => 'my-block-name']) ?>> <h1><?php echo get_the_title() ?></h1> <h2><?php echo $attributes['someCustomAttribute'] ?> // and imagine here you want to get some datas from another post, taxonomy, related-post-type <div class="my-block-name__content"> <?php echo $content ?> </div> </section>
This example works well, but cause problem when trying to migrate dynamic datas to static datas.
Using custom attibutes inside the blocks would fix the migration problem, but then needs the user to update each post the block is used in ? (lest’s imagine there are 30,40,50 posts …)
I have seen other examples (like here), and I was then wondering if this approach is ok or not ? Is there a clean(er) way to use both dynamic datas and static innerBlocks ?
- The topic ‘Use innerBlocks in a gutenberg dynamic Block ?’ is closed to new replies.