0

Imagine this simple

 $items = array(
   'string',
   'string2',
   $extra_items,
 );

And theme a block content like

 $content = array(
   'subject'   => t('Block title'),
   'content'   => array(
     '#theme'  => 'item_list',
     '#items'  => $items,
     '#attributes' => array('class' => 'my-UL'),
   ),
 );

$extra_items is getting values from a custom hook that may be empty!In that case theme_item_list() brings an empty <li></li> for that item that is NULL

How can i ensure that this <li></li> is displayed only when this item has a value?

  • 2
    I'm not sure I understand. Why can't you run array_filter or similar on $items ? – Alice Heaton Mar 22 '13 at 16:14
  • I could try that, but it seems weird that theme_item_list doesn't take care of that.. – Pan Chrono Mar 23 '13 at 9:03
  • @AliceHeaton array_filter() worked perfectly, if you convert your comment to an answer i 'd be happy to accept it, cheers – Pan Chrono Mar 26 '13 at 14:33
0

You simply need to run array_filter on $items - this will remove the empty entries.

Whether this should be handled by theme_item_list or not is debatable. It doesn't make semantic sense to theme empty list items - however in most cases the filtering would not be necessary, so from a performance point of view it doesn't make sense for theme_item_list to do it.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.