4

So, I'm trying to generate some markup using render arrays. I need to create some nested divs. I thought that I could do this by nesting the arrays, as such:

function make_array() {
  $items = port_view_get_block();
  $array['container']['#prefix'] = '<div id = "container">';
  $array['container']['#suffix'] = '</div>';
  $array['container']['#markup'] = 'This is inside the container div';
  $array['container']['slider']['#prefix'] = '<div id = "slider">';
  $array['container']['slider']['#suffix'] = '</div>';
  $array['container']['slider']['#markup'] = 'This is inside the slider div';
  return $array;
}

This doesn't seem to work. The inner array isn't rendered.

What is the proper way to get a nested structure using render arrays?

10

If an element is given #markup, the render process stops searching for nested elements. To accomplish what you need, just create a second child element with no #prefix or #suffix.

function make_array() {
  $items = port_view_get_block();
  $array = array(
    'container' => array(
      '#prefix' => '<div id="container">',
      '#suffix' => '</div>',
      'markup' => array(
        '#markup' => 'This is inside the container div',
      ),
      'slider' => array(
        '#prefix' => '<div id="slider">',
        '#suffix' => '</div>',
        '#markup' => 'This is inside the slider div',
      ),
    ),
  );
  return $array;
}
1

We need more to code to see your problem because this, indeed, should render as you want it. Perhaps there's some theme function or something. Try using your function as a page callback for a test.

  • I'm passing this to $block['content'] directly - maybe I need to pass this to a theme function first? – starsinmypockets Sep 17 '11 at 15:45

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.