Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
Add pseudo-code-ish wp-each implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Mar 2, 2023
1 parent bfb6ad8 commit 980fb0f
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/directives/attributes/wp-each.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

require_once __DIR__ . '/../class-wp-directive-processor.php';

function process_wp_each( $tags, $context ) {
if ( $tags->is_tag_closer() ) {
return;
}

$prefixed_attributes = $tags->get_attribute_names_with_prefix( 'wp-each:' );
if ( 0 === count( $prefixed_attributes ) ) {
return;
}
$attribute_name = $prefixed_attributes[0];

list( , $iterator_name ) = explode( ':', $attribute_name );

$value = $tags->get_attribute( $attribute_name );
if ( null === $value ) {
// No wp-each directive.
return;
}

$loop_array = evaluate( $value, $context->get_context() );
// TODO: Error handling.

$loop_inner_html = '';
foreach ( $loop_array as $iteration_item ) {
$context->set_context( array( 'item' => $iteration_item ) );

$inner_html = $tags->get_inner_html();
$inner_tags = new WP_Directive_Processor( $inner_html );
$inner_tags = wp_process_directives( $inner_tags, $context );
$loop_inner_html .= $inner_tags->get_inner_html();

$context->rewind_context();
}
$tags->set_inner_html( $loop_inner_html );
$tags->find_balanced_tag();
}

0 comments on commit 980fb0f

Please sign in to comment.