Undefined var w/ sage @foreach, but not vanilla foreach()

Good suggestion, @alwaysblank. Not sure if it gets us anywhere, but here’s what the empty look from the initial example compiles to (cleaned up for readability):

<?php
$__currentLoopData = $related_products;
$__env->addLoop($__currentLoopData);

foreach($__currentLoopData as $related_product):
  $__env->incrementLoopIndices();
  $loop = $__env->getLastLoop();
  echo e($related_product);
endforeach;

$__env->popLoop();
$loop = $__env->getLastLoop();
?>

So yeah, there’s plenty there that’s added by Blade.

@shaneparsons, I think $__env is supposed to be an instance of Illuminate\View\Factory and basically have a lot of data and functionality that Blade needs to work. For some reason, it’s not being defined when your template is loaded.

Re this post Why can't I foreach my var?, how is your related products Blade partial being loaded? For example, is it being called via @include from a full Blade template? By that I mean a template that corresponds to a location in the WordPress template hierarchy, like single-product.blade.php.

1 Like