Custom loops

This seems like a dumb question, even to me, but I have no idea how to loop items outside the designated views where $items is already defined and cannot find any useful and clear documentation of how custom looping works.

For example, what if I need to loop through items that fit a certain criteria (e.g. in a specific collection) from within, say, the homepage, or the items/tags template, or the footer?

Shouldn't something like the following always work?

$myItems=get_records('item', array('hasImage'=>true, 'featured'=>true),10);
set_loop_records('items', $myItems);
foreach(loop('items') as $item)
	// do something;
endforeach;

I can't seem to find a method that works everywhere. What am I missing?

Ok, looks like I had some minor issues with the above...

I forgot the $items = get_loop_records('Items') part (and also had a missing colon).

The following seems to work fine.

$myItems=get_records('Item', array('hasImage'=>true, 'featured'=>true),10);
set_loop_records('Items', $myItems);
$items = get_loop_records('Items');
foreach($items as $item){
	// do something
}