Let's have a block that creates a Carousel using <InnerBlocks />
:
edit.js
of the parent component returns something like:
return(
<div className="carousel">
<div className="carousel__track">
<ul {...innerBlocksProps} className="carousel__list" />
</div>
</div>
)
edit.js
of the child returns something like:
return(
<li class="slide">
<figure>
<img src={url} data-id={id} alt={title} />
<figcaption>
<h5>{caption}</h5>
</figcaption>
</figure>
</li>
)
In the parent edit.js
I would create a state like; const [isPreview, setIsPreview] = useState(false)
and when I click a "edit" button the state is set to false
, when I click a "preview" button the state is set to true
.
I thought that when the parent state isPreview
is true
I could render the Carousel via <ServerSideRender />
, so I've created a server-side rendered block, let's call it "myblocks/carousel-preview" and I've changed the edit.js
parent like that:
return(
{
!isPreview
?
<div className="carousel">
<div className="carousel__track">
<ul {...innerBlocksProps} className="carousel__list" />
</div>
</div>
)
:
<>
<ServerSideRender
block="myblocks/carousel-preview"
attributes={{ /* pass children here?!? */ }}
/>
</>
My main problem is: how can I pass to my <ServerSideRender />
block the children of my block?
In fact, I thought that the markup of the <ServerSideRender />
could be something like:
<?php
function RenderCarouselServerSide($attributes, $content) {
ob_start();
?>
<div class="carousel">
<div class="carousel__track">
<ul class="carousel__list" />
<?php foreach($attributes['children'] as $child) :
$url = $child['url'];
$id = $child['id'];
$caption = $child['caption'];
?>
<li class="slide">
<figure>
<img src="<?php echo $url; ?>" data-id="<?php echo $id; ?>" />
<figcaption>
<h5>"<?php echo $caption; ?>"</h5>
</figcaption>
</figure>
</li>
</ul>
<?php endforeach; ?>
</div>
</div>
<?php
return ob_get_clean();
}
But how to pass the children as $attributes
?