Mixing PHP function call and HTML in a template foreach loop

As you have discovered, jumping between php and templates is a real pain especially when quoting is involved. That is one of the reasons why template engines such as Twig are so popular. However, I know you want to understand all the details.

I would suggest switching to the php heredoc string notation which not only reduces having to switch between php and templating but pretty much eliminates the quoting issue.

I would also suggest that you start to move some code into dedicated functions. I think it will help in debugging. So here is a working example:

<?php

$groups = [
    ['id' => 1, 'name' => 'image1'],
    ['id' => 2, 'name' => 'image2'],
];
foreach($groups as $group) {
    echo render_group_image($group) . "\n";
}
function render_group_image($group) : string {
    $html = <<<EOT
<img src="/img/{$group['id']}.jpg">{$group['name']}</img>
EOT;
    return $html;
}

I used arrays here just to keep the example simple but $group->id will work just fine as well for objects.

I might add that your really should be escaping your html output but that is a refinement that can be added later.