Incrementally build HTML while looping through pages in page_list

Permalink
I'm hoping someone here can point me in the right direction (an example would be great!). I'm working on a template for the page_list block. In the template, I want to loop through the blocks on the page and grab values from them and assign them to variables in my code.

Then, for each page, I want to incrementally build up a block of HTML that contains information parsed from out of the block(s) on the page. For instance, let's say I have a block that I've created that has two fields on it: a text field for some short caption text and an image selector to pick an image out of the system. I'd like to end up with a block of HTML like this:
<div class="thumbnail">
   <img src="path-to-the-image-chosen" alt="text-from-the-text-field" />
   <p class="caption">text-from-the-text-field</p>
</div>

I'm trying to build my way into it... and started by trying to just grab the text (thought that would be easier than trying to deal with the image!).
<?php foreach ($pages as $page): 
   $blocks = $page->getBlocks('Main');
   if(!empty($blocks)) {
      foreach($blocks as $block) {
         $btc = $block->getInstance();
         $caption = $btc->field_2_textbox_text;
      }
   }
   $htmlDisplay .= '<div class="thumbnail" style="display: none;">';
   $htmlDisplay .= '<p>';
   $htmlDisplay .= $name;
   $htmlDisplay .= '</p>';
   $htmlDisplay .= '</div>';
?>
<?php endforeach; ?>

Rhe echo command is outputting all of the HTML text strings, but no name, even though I KNOW that the page has a block that has that information on it. :(

Also... is there a better way to concatenate a whole bunch of text strings (HTML) and PHP variables together?

Thanks!

- John

arrestingdevelopment
 
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
$htmlDisplay .= $caption; // not $name;
arrestingdevelopment replied on at Permalink Reply
arrestingdevelopment
WHOOPS! Thanks for catching that, John!

I also found that part of my problem was the controller for the block. In its "view" function, it wasn't providing access to the block fields that I needed... specifically "field_2_textbox_text". As created, the controller.php only had this:
public function view() {
   $this->set('field_4_image', (empty($this->field_4_image_fID) ? null : $this->get_image_object($this->field_4_image_fID, 270, 270, true)));
   $this->set('field_5_wysiwyg_content', $this->translateFrom($this->field_5_wysiwyg_content));
}

So I had to add to it so it was like this:
public function view() {
   $this->set('field_2_textbox_text', (empty($this->field_2_textbox_text) ? null : $this->field_2_textbox_text));
   $this->set('field_3_textbox_text', (empty($this->field_3_textbox_text) ? null : $this->field_32_textbox_text));
   $this->set('field_4_image', (empty($this->field_4_image_fID) ? null : $this->get_image_object($this->field_4_image_fID, 270, 270, true)));
   $this->set('field_5_wysiwyg_content', $this->translateFrom($this->field_5_wysiwyg_content));
}

Once I did that, VOILA! Getting the value was a snap:
$caption = $btc->field_2_textbox_text

HUZZAH!

- John