Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php

/**
 * Theming a common wrapper to supply html attributes without modifying templates.
 *
 * Wrapper templates have an extension of ".wrapper.php".
 */

/**
 * These variables will be available to all templates owned by this theme or
 * any sub-themes.
 */
function THEME_NAME_preprocess(&$vars, $hook) {
  global $theme_key;


  // From general to more specific, add these wrapper paths and template names.
  // Applies to all templated hooks. More can be added through hook specific
  // preprocess but these should be good enough for most cases. We want less
  // templates, not more.

  // Add paths to your wrapper templates. The default will sit in the base of
  // your theme. path_to_theme() not used since it can lead to module owned templates.
  $vars['template_wrapper_paths'][] = drupal_get_path('theme', $theme_key);
  $vars['template_wrappers'][] = 'common';
  $vars['template_wrappers'][] = $hook;

  // Element attributes can be added either way but this is easier to enter...
  $vars['wrapper_id']        = '';
  $vars['wrapper_classes'][] = $hook . '-wrapper';
  // ...While this structure allows passing through drupal_attributes().
  $vars['wrapper_attributes_array']['id']    = &$vars['wrapper_id'];
  $vars['wrapper_attributes_array']['class'] = &$vars['wrapper_classes'];

  // Simple switch to trigger.
  // You definitely don't want this on the page hook.
  $vars['enable_wrapper'] = $hook !== 'page' ? TRUE : FALSE;
}



/**
 * Override of theme_render_template. This allows for template wrappers.
 */
function phptemplate_render_template($template_file, &$variables) {

  process_template_wrapper($variables);

  // Template render.
  extract($variables, EXTR_SKIP && EXTR_REFS);
  ob_start();
  include "./$template_file";
  $contents = ob_get_contents();
  ob_end_clean();

  // Template wrapper.
  if (!empty($variables['template_wrapper']) && !empty($contents)) {
    ob_start();
    include './' . $variables['template_wrapper'];
    $contents = ob_get_contents();
    ob_end_clean();
  }

  return $contents;
}



/**
 * A little helper to find the right wrapper template and to clean html attributes.
 */
function process_template_wrapper(&$variables) {

  $variables['template_wrapper'] = $variables['wrapper_attributes'] = '';
  if (!empty($variables['enable_wrapper']) && $template_wrapper = drupal_discover_template($variables['template_wrapper_paths'], $variables['template_wrappers']), '.wrapper.php') {

    $attributes = array();
    foreach ($variables['wrapper_attributes_array'] as $attribute => $value) {
      if (empty($value)) {
        continue;
      }
      if (is_array($value)) {
        $value = implode(' ', $value);
      }
      $attributes[$attribute] = $value;
    }

    $variables['wrapper_attributes'] = drupal_attributes($attributes);
    $variables['template_wrapper'] = $template_wrapper;
  }

}

?>



<?php
/**
 * Within 'common.wrapper.php' or 'HOOK.wrapper.php':
 */
?>

<div<?php print $wrapper_attributes ?>>
 <?php print $contents ?>
</div>