2

I have a custom module that implements hook_block_view. I would like to know where should I put extensive html code for a block content.

For example:

$block['title'] = t('TEST');
$block['content'] = '<div id="user-custom-block">';
$block['content'] .= ... 50 LINES OF HTML.
$block['content'] .= '</div>';

What I did so far was to create a .tpl.php file and put all this code there, and then only call:

$block['title'] = t('TEST');
$block['content'] = theme('user_custom_block');

Is it correct?

If yes, so far, so good. But what if I have a "complex" logic inside of this block content?

For example:

$block['title'] = t('TEST');
for...
  $block['content'] = '<div id="user-custom-block">';
  if:
    $block['content'] .= ... 50 LINES OF HTML.
  endif;
  $block['content'] .= '</div>';
endfor;
etc
etc

Is this still ok to use in a .tpl.php file? If not, which approach should I use?

Should I use something like this?

/**
 * Implementation of hook_block_view().
 */
function custom_block_view($delta='') {
  $block = array();
  switch ($delta) {
    case 'my-block-id':
      $block['subject'] = t('Block Name');
      $block['content'] = custom_contents();
      break;
  }
  return $block;
}

/**
 * custom html block
 * @return string
 */
function custom_contents() {
  return '
      </p><div class="body">
        Hello World, this is a example custom Block
      </div><p>
    ';
}
  • 1
    I would personally use a .tpl.php file but i believe both is ok. – chadpeppers Jul 29 '14 at 19:46
1

Your goal should be to keep all processing out of the template file. Treat the template as a template - just some HTML and in some places, you print out variables.

From what you are describing though, it would be most useful to use a template for the '50 lines of HTML'. It is one thing to maintain a few lines of HTML in a PHP function, but 50 sounds like a nightmare.

<!-- my-template.tpl.php -->
<div class="50-lines-of-html">
  <?php echo $this; ?>
  <?php echo $that; ?>
</div>

Next, create some function that runs your for ... loop and builds the content using that template:

function process_my_things($things) {
  $output = '';
  foreach ($things as $thing) {
    if ($it_is == TRUE) {
      // do some stuff with $thing
      $output .= theme('my-template', array('this' => $this, 'that', => $that));
    }
  }
  return $output
}

Note that this can be a theme function too, but it doesn't have to be unless you want to allow overrides.

Finally, use another theme function or a template as a wrapper to arrange what you've got. You could even just use $block['content'] here as above:

$block['content'] = '<div id="my-wrapper">';
$block['content'] .= process_my_things($things);
$block['content'] .= '</div>';
1

You could improve that with 2 different options: 1) Creating a theme function 2) Creating a template file (tpl)

The tpl may be a cleaner option, but they are both similar. In both cases, you should pass preprocessed variables instead of putting the logic inside the theme function/tpl file.

Also, note that the literal strings should go through the t() function.

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.