0

I've defined a block that returns a string of HTML as follows:

function product_block_view($delta = '')
{
  switch ($delta) {
    case 'groups_images':
      $block['subject'] = t('Category Images in Groups');
      $block['content'] = "<div>Group images html</div>";
      break;

    case 'singles_images':
      $block['subject'] = t('Category Images as Singles');
      $block['content'] = "<div>Single images html</div>";
      break;

    default:
      break;
  }

  return $block;
}

This block lives in a custom region named 'myregion'. I am attempting to render this block's view with <?php render($page['myregion']) ?> but nothing comes through. The dump of $page['myregion'] looks something like this:

    array (size=4)
        'product_groups_images' => 
            array (size=5)
              '#markup' => string '<div>Groups images html</div>' (length=29)
              '#contextual_links' => 
                array (size=1)
                  'block' => 
                    array (size=2)
                      0 => string 'admin/structure/block/manage' (length=28)
                      1 => 
                        array (size=2)
                          ...
              '#block' => 
                object(stdClass)[116]
                  public 'bid' => string '77' (length=2)
                  public 'module' => string 'product' (length=7)
                  public 'delta' => string 'groups_images' (length=13)
                  public 'theme' => string 'id' (length=2)
                  public 'status' => string '1' (length=1)
                  public 'weight' => string '0' (length=1)
                  public 'region' => string 'findyourflavor' (length=14)
                  public 'custom' => string '0' (length=1)
                  public 'visibility' => string '1' (length=1)
                  public 'pages' => string '<front>' (length=7)
                  public 'title' => string '' (length=0)
                  public 'cache' => string '1' (length=1)
                  public 'subject' => string 'Category Images in Groups' (length=25)
              '#weight' => int 1
              '#theme_wrappers' => 
                array (size=1)
                  0 => string 'block' (length=5)
        '#sorted' => boolean true
        '#theme_wrappers' => 
            array (size=1)
              0 => string 'region' (length=6)
        '#region' => string 'findyourflavor' (length=14)

Any idea what I'm doing wrong that is causing my div to not be rendered by the render function?

2

You forgot to print the results. Use:

<?php print render($page['myregion']) ?>

Explanation:

During a page request, Drupal keeps all the data it loads in PHP arrays for as long as possible. The reason for this is, that as long as the data is in arrays, it can be changed by other elements. This is the power of Drupal's module system.

The array posted in the question is such a data array, it's called a "Render array". Eventually this array must be converted to HTML, this is what render does: it converts a drupal render array to an HTML string. Calling render often happens at end of a page request.

So calling render gives you an HTML string that should be returned to the user, e.g. by printing it in a template file.

  • Oh my I hope thats not it... – Noah Duncan May 15 '14 at 18:56
  • @Jesse Can you take a pass at expanding this a little, and explain how render() only returns the rendered array, and that you need to print it. An explanation will help future readers more. Thanks. – mpdonadio May 15 '14 at 20:40
  • I tried to clarify it a bit – Jesse May 15 '14 at 20:59
  • Thanks for the detail Jesse. That makes sense. I think I got confused by the name, I think of "render" as meaning "render on page" which would imply a print. It makes sense that a framework wouldn't do this by default though, so +1 to drupal doing things right, -1 for using "render" as the name ;) – Noah Duncan May 16 '14 at 23:36

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.