How to theme multiple entities on the same page? I am trying to write a module that displays several entities in one page, but I don't see how I should theme them.

I've found (an mostly followed) this: https://drupal.org/node/1238606.

But the point where I get stuck is the return value of the page callback. The link above suggests:

function my_entity_page_view($entity, $view_mode='full') {
  $entity_type = $entity-&gt;entityType();
  $entity_id = entity_id($entity_type, $entity);

  $entity-&gt;content = array();
  $entity-&gt;title = filter_xss($entity-&gt;title);

  field_attach_prepare_view($entity_type, array($entity_id =&gt; $entity), $view_mode);
  entity_prepare_view($entity_type, array($entity_id =&gt; $entity));
  $entity-&gt;content += field_attach_view($entity_type, $entity, $view_mode);

  $entity-&gt;content += array(
    '#theme'     =&gt; $entity_type,
    '#element'   =&gt; $entity,
    '#view_mode' =&gt; $view_mode,
    '#language'  =&gt; LANGUAGE_NONE,
  );
  return $entity-&gt;content;
}


I however try to load several entities at once and have no fields, so I tried this:

$entities = // Array of entities keyed by id;

entity_prepare_view('entity_type', $entities);

$return = array(
  '#theme'     =&gt; 'entity_type',
  '#element'   =&gt; $entities,
  '#view_mode' =&gt; 'full',
  '#language'  =&gt; LANGUAGE_NONE,
);

return $return;


Since entity_prepare_view() accepts an array of entities, I'd suppose there should be a way to theme several entities at once as well. Or am I wrong?
After a bit more fiddling, this worked for me. I've tried a similar approach earlier, but I guess there was something wrong. I've commented out the field functions, but these are the places in case you need them.

$entities = // Array of entities keyed by id;

// Prepare all entities at once. Attach fields if desired.
//field_attach_prepare_view('entity_type', $entities, 'full');
entity_prepare_view('entity_type', $entities);

// Expose all entities individual to hook_theme.
foreach($entities as $key =&gt; $entity) {
  $entity-&gt;content = array();
  //$entity-&gt;content += field_attach_view('entity_type', $entity, 'full');
  $entity-&gt;content += array(
    '#theme'     =&gt; 'entity_type',
    '#element'   =&gt; $entity,
    '#view_mode' =&gt; 'full',
    '#language'  =&gt; LANGUAGE_NONE,
  );
  $entities[$key] = $entity-&gt;content;
}

return $entities;