Как заставить заработать foreach в template.php

Главные вкладки

Аватар пользователя volocuga@drupal.org volocuga@drupal.org 4 июля 2010 в 23:06

У меня есть функция в template.php, которая, помимо прочего, должна извлекать циклом картинки из поля imagecache и передавать в шаблон node.tpl.php

Вот кусок кода, что у меня есть в template.php

<?php

function small_images($node) {
        global 
$base_path
    
$index 0
        foreach (
$node->field_image_cache as $image) {
        
$index++;
          
$filepath $image['filepath'];
          
$alt $image['data']['alt'];
          
$title $image['data']['title'];
          if (
$index 0) {          
      
$output '<div class="image-thumb"><a href="'.$base_path.$filepath.'" alt="'.$alt.'"  title="'.$title.'">'.theme('imagecache''uc_thumbnail'$filepath$alt$title).'</a></div>';
     } 
   }
   return 
$output);  
}

?>
<?php

echo small_images($node)

?>

в node.tpl.php

выдаёт всего одно изображение вместо нескольких. В то же время, если этот цикл вставить напрямую в ноду, всё показывает как надо.

Как отображать все картинки? По ряду причин не хочу вставлять цикл напрямую в шаблоне.

Комментарии

Аватар пользователя xxandeadxx xxandeadxx 4 июля 2010 в 23:49
<?php
function small_images($node) {
  global 
$base_path
  
$index 0
  
$output ''// <--- fix

  

foreach ($node->field_image_cache as $image) {
    
$index++;
    
$filepath $image['filepath'];
    
$alt $image['data']['alt'];
    
$title $image['data']['title'];
    if (
$index 0) {          
      
$output .= '<div class="image-thumb"><a href="'.$base_path.$filepath.'" alt="'.$alt.'"  title="'.$title.'">'.theme('imagecache''uc_thumbnail'$filepath$alt$title).'</a></div>'// <--- fix
    

  }

  return 

$output// <--- fix
}
?>