Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Looping Templates in foreach() only returns final template #44

Open
ThomasDeer opened this issue Aug 16, 2022 · 2 comments
Open

Looping Templates in foreach() only returns final template #44

ThomasDeer opened this issue Aug 16, 2022 · 2 comments

Comments

@ThomasDeer
Copy link

I'm trying to allow ordering of which templates get shown through a foreach loop, (essentially i'm building a slider thats generated through lots of template parts being pulled together, very specific use case but it has standard slides built into the plugin, then these will be overwritable within the theme)

however my foreach loop is only returning the final item
foreach ($orderArr as $template_slug) { $my_template_loader->set_template_data( $userData )->get_template_part( $template_slug ); }

My orderArr is Array ( [0] => welcome [1] => slide1 [2] => slide2 )

However if i manually call the template parts
$my_template_loader->set_template_data( $userData )->get_template_part( 'welcome' ); $my_template_loader->set_template_data( $userData )->get_template_part( 'slide1' ); $my_template_loader->set_template_data( $userData )->get_template_part( 'slide2' );
It all works as expected.

Any suggestions would be helpful

@GaryJones
Copy link
Owner

Interesting. I would have assumed that your foreach would have worked correctly.

Totally untested, but would using the yield keyword work here?

$orderArr = [ 'welcome', 'slide1', 'slide2' ];
foreach ( $orderArr as $template_slug ) {
	yield $my_template_loader->set_template_data( $userData )->get_template_part( $template_slug );
}

@ThomasDeer
Copy link
Author

Just given this (yield) a go and appears to have the same behaviour, if I echo the template name after the get_template_part (in my original code)

$my_template_loader->set_template_data( $userData )->get_template_part( $template_slug ); echo $template_slug;

The page renders with welcome slide1 (slide 2 complete content) slide2,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants