Passing variables into a foreach loop in a template

Hey Guys,

I have a foreach loop that uses Wordpress’s get_term_by function, in my partial template I have the following:

@php
	$queried_object = get_queried_object();
	$term_id = $queried_object->term_id;
	$taxonomy_name = 'videos_category';
	$term_children = get_term_children( $term_id, $taxonomy_name );
@endphp

<nav class="filter">
    <ul>
        @foreach ( $term_children as $child ) 
            @php
                $term = get_term_by( 'id', $child, $taxonomy_name );
                echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '" class="btn btn--sml">' . $term->name . '</a></li>';
            @endphp
        @endforeach
    </ul>
</nav>

To remove some of this logic, i’d put my variables into a controller but how can I pass those variables from my controller, into the foreach loop in my template?

Cheers!

I wrote a little bit here: ACF variables in blade template. Basically performing the full logic/loop in the controller then building a new array or object to loop through in the template which contains only the data you’re going to use.

2 Likes

Smart dude, cheers for that :slight_smile:

1 Like

Sorry @nathobson,

I can’t seem to get anything outputting, giving the php in my post above, I tried using the method you had illustrated in the post you mentioned.

Here is what I have:
Controller:

    public function videoCategoryFilter()
    {
        $info = [];

        $queried_object = get_queried_object();
        $term_id = $queried_object->term_id;
        $taxonomy_name = 'videos_category';
        $term_children = get_term_children( $term_id, $taxonomy_name );

        foreach ( $term_children as $child ) {
            $subcategory = (object) array(
                'name' => get_term_by( 'id', $child, $taxonomy_name ),
            );

            array_push($info, $subcategory);
        }

        return $info;
    }

View:

			@foreach ($videoCategoryFilter as $child)
				{{ $child->name }}
			@endforeach

Am I completely off? :stuck_out_tongue:

The variable you need to use is $video_category_filter (i.e. no camelCase).

2 Likes

ahhh I see! so we can’t use camel case at all? in App.php the siteName function works…not sure why.

I’ve fixed it to use underscores but I seem to get an issue with using the name param

Warning: htmlspecialchars() expects parameter 1 to be string, object given in /Users/mattellis/Documents/sites/wordpress/wp-content/themes/video-marketing-strategy_theme/vendor/illuminate/support/helpers.php
    public function video_category_filter()
    {
        $info = [];

        $queried_object = get_queried_object();
        $term_id = $queried_object->term_id;
        $taxonomy_name = 'videos_category';
        $term_children = get_term_children( $term_id, $taxonomy_name );

        foreach ( $term_children as $child ) {
            $subcategory = (object) array(
                'name' => get_term_by( 'id', $child, $taxonomy_name ),
                'link' => get_term_link( $child, $taxonomy_name ),
            );

            array_push($info, $subcategory);
        }

        return $info;
    }

view:

        @foreach ($video_category_filter as $child)
            {{ $child->link }}
            <p>{{ $child->name }}</p>
        @endforeach

The original php looked like this when it was working:


<?php
	$queried_object = get_queried_object();
	$term_id = $queried_object->term_id;
	$taxonomy_name = 'videos_category';
	$term_children = get_term_children( $term_id, $taxonomy_name );

	echo '<ul>';
	foreach ( $term_children as $child ) {
		$term = get_term_by( 'id', $child, $taxonomy_name );
		echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a></li>';
	}
	echo '</ul>';
?>

Controller converts the cameCase of your method names into snake_case when it turns them into variables that are made available to your blades. You’d have to ask @withjacoby why exactly it does that.

As for your htmlspecialchars error: get_term_by returns an object, which you assigned to name. When you try and access it with {{ $child->name }} you’re attempting to treat an object as a string, and htmlspecialchars() chokes on that.

You can probably solve that issue by making the following change:

foreach ( $term_children as $child ) {
    $subcategory = (object) array(
        // get the name from what `get_term_by()` returns:
        'name' => get_term_by( 'id', $child, $taxonomy_name )->name,
        'link' => get_term_link( $child, $taxonomy_name ),
    );

    array_push($info, $subcategory);
}
2 Likes

Hey @alwaysblank and @mellis84,

It’s following PSR-1 basic coding standards. Method names should be in camel case as per the spec.

http://www.php-fig.org/psr/psr-1/

2 Likes

Thanks @alwaysblank that fixed the issue, makes a lot more sense now, really appreciate it. :smiley:

1 Like