8 Ways to Display the Contents of a Block: simple markup, using theme function, using template file, and integration with Render API (#markup and #theme)

Public

Ideas were obtained from my Drupal Summer Camp PH 2015 presentation:
http://www.slideshare.net/ranelpadon/the-synergy-of-drupal-hooksapis-imp...

And the corresponding GitHub repo for the custom module I've implemented:
https://github.com/ranelpadon/summercamp

Get raw version
php
  1. function HOOK_block_info() {
  2. $blocks = array();
  3.  
  4. $blocks['block_A'] = array(
  5. 'info' => t('Block A'),
  6. );
  7.  
  8. $blocks['block_B'] = array(
  9. 'info' => t('Block B'),
  10. );
  11.  
  12. $blocks['block_C'] = array(
  13. 'info' => t('Block C'),
  14. );
  15.  
  16. $blocks['block_D'] = array(
  17. 'info' => t('Block D'),
  18. );
  19.  
  20. $blocks['block_E'] = array(
  21. 'info' => t('Block E'),
  22. );
  23.  
  24. $blocks['block_F'] = array(
  25. 'info' => t('Block F'),
  26. );
  27.  
  28. $blocks['block_G'] = array(
  29. 'info' => t('Block G'),
  30. );
  31.  
  32. $blocks['block_H'] = array(
  33. 'info' => t('Block H'),
  34. );
  35.  
  36. return $blocks;
  37. }

Block Markups when Displayed

Get raw version
php
  1. function HOOK_block_view($block_name = '') {
  2. $block = array();
  3.  
  4. $user_name = 'Ranel';
  5.  
  6. switch ($block_name) {
  7. case 'block_A':
  8. // Simple text markup, no HTML tags.
  9. $block['content'] = t('Hello ') . $user_name;
  10.  
  11. break;
  12.  
  13. case 'block_B':
  14. // Simple text markup, with HTML tags.
  15. $block['content'] = '<p>' . t('Hello ') . $user_name . '</p>';
  16.  
  17. break;
  18.  
  19. case 'block_C':
  20. // Using a function definition, rendered using theme function.
  21. $block['content'] = theme('function_A', array('user_name' => $user_name));
  22.  
  23. break;
  24.  
  25. case 'block_D':
  26. // Using a template file contents, rendered using theme function.
  27. $block['content'] = theme('file_A', array('user_name' => $user_name));
  28.  
  29. break;
  30.  
  31. case 'block_E':
  32. // Using a function definition, rendered using Render API's #markup.
  33. $block['content']['#markup'] = theme('function_A', array('user_name' => $user_name));
  34.  
  35. break;
  36.  
  37. case 'block_F':
  38. // Using a template file contents, rendered using Render API's #markup.
  39. $block['content']['#markup'] = theme('file_A', array('user_name' => $user_name));
  40.  
  41. break;
  42.  
  43. case 'block_G':
  44. // Using a function definition, rendered using Render API's #theme.
  45. $block['content'][] = array (
  46. '#theme' => 'function_A',
  47. '#user_name' => $user_name,
  48. );
  49.  
  50. break;
  51.  
  52. case 'block_H':
  53. // Using a template file contents, rendered using Render API's #theme.
  54. $block['content'][] = array (
  55. '#theme' => 'file_A',
  56. '#user_name' => $user_name,
  57. );
  58.  
  59. break;
  60. }
  61.  
  62. return $block;
  63. }

Registration of the Theming Mechanisms

Get raw version
php
  1. function HOOK_theme() {
  2. return array(
  3. 'function_A' => array(
  4. 'variables' => array(
  5. 'user_name' => NULL,
  6. ),
  7. ),
  8.  
  9. // Template file is file_A.tpl.php, assumed to be in the root directory of the module.
  10. 'file_A' => array(
  11. 'template' => 'file_A',
  12. 'variables' => array(
  13. 'user_name' => NULL,
  14. ),
  15. ),
  16. );
  17. }

function_A Definition

Get raw version
php
  1. function theme_function_A($vars) {
  2. $user_name = $vars['user_name'];
  3.  
  4. return '<p>' . t('Hello ') . $user_name . '</p>';
  5. }

file_A Template File (file_A.tpl.php)

Get raw version
php
  1. <p><?php print t('Hello ') . $user_name; ?></p>