Repeater

Class.

use WebCodingCat\WpAppFramework\Block\Repeater;

Renders the inner blocks of a block once per item: a post, a term, a comment, a user or a row of a table, like core/post-template does for posts. A block receives it as parameter of render() instead of string $content; which items there are, the block decides, e.g. with a query.

public function render(EventListAttributes $attrs, Repeater $repeater): string
{
    $items = array_map($repeater->post(...), Event::query()->limit($attrs->count)->get());

    return $this->renderer->render('Block/EventList/EventList.twig', ['items' => $items]);
}

Each item gives the inner blocks the block context of the WordPress loop blocks – postId and postType, termId and taxonomy, commentId –, or userId for a user and {namespace}/{table} for a row; blocks of the theme or plugin read the item with #[Item], core blocks such as core/post-title with their own context. The context of the ancestors stays available. For a post, the global post is set while its item renders.

In renders for the block editor the items are empty: the editor shows the inner blocks of the first item, or the one clicked, to edit, and the others as previews.

Methods

comment()

comment(WP_Comment|int $comment): RepeaterItem

The inner blocks for a comment, with the context commentId.

ParameterTypeDefaultDescription
commentint|WP_CommentrequiredThe comment or its ID.

Returns RepeaterItem

Throws InvalidArgumentException: If the comment does not exist.

post()

post(WP_Post|AbstractPost|int $post): RepeaterItem

The inner blocks for a post, with the context postId and postType and the post as global post.

ParameterTypeDefaultDescription
postint|WP_Post|AbstractPostrequiredThe post, its ID or its post object.

Returns RepeaterItem

Throws InvalidArgumentException: If the post does not exist.

row()

row(AbstractRow $row): RepeaterItem

The inner blocks for a row of a table of a theme or plugin, with the context {namespace}/{table} (the primary key).

ParameterTypeDefaultDescription
rowAbstractRowrequiredA saved row object.

Returns RepeaterItem

Throws InvalidArgumentException: If the row has not been saved yet.

term()

term(WP_Term|int $term): RepeaterItem

The inner blocks for a term, with the context termId and taxonomy.

ParameterTypeDefaultDescription
termint|WP_TermrequiredThe term or its ID.

Returns RepeaterItem

Throws InvalidArgumentException: If the term does not exist.

user()

user(WP_User|int $user): RepeaterItem

The inner blocks for a user, with the context userId.

ParameterTypeDefaultDescription
userint|WP_UserrequiredThe user or their ID.

Returns RepeaterItem

Throws InvalidArgumentException: If the user does not exist.