7 theme.inc theme_html_tag($variables)

Returns HTML for a generic HTML tag with attributes.

Parameters

$variables: An associative array containing:

  • element: An associative array describing the tag:

    • #tag: The tag name to output. Typical tags added to the HTML HEAD:

      • meta: To provide meta information, such as a page refresh.
      • link: To refer to stylesheets and other contextual information.
      • script: To load JavaScript.
    • #attributes: (optional) An array of HTML attributes to apply to the tag.
    • #value: (optional) A string containing tag content, such as inline CSS.
    • #value_prefix: (optional) A string to prepend to #value, e.g. a CDATA wrapper prefix.
    • #value_suffix: (optional) A string to append to #value, e.g. a CDATA wrapper suffix.

Related topics

1 theme call to theme_html_tag()
drupal_get_js in includes/common.inc
Returns a themed presentation of all JavaScript code for the current page.
7 string references to the theme hook from theme_html_tag()

Note: this list is generated by looking for the string for this theme hook, so it may include some references that are not actually using this theme hook.

drupal_add_html_head in includes/common.inc
Adds output to the HEAD tag of the HTML page.
drupal_common_theme in includes/common.inc
Provides theme registration for themes across .inc files.
drupal_get_js in includes/common.inc
Returns a themed presentation of all JavaScript code for the current page.
drupal_pre_render_styles in includes/common.inc
#pre_render callback to add the elements needed for CSS tags to be rendered.
RenderElementTypesTestCase::testHtmlTag in modules/simpletest/tests/theme.test
Tests system #type 'html_tag'.

... See full list

File

includes/theme.inc, line 2257
The theme system, which controls the output of Drupal.

Code

function theme_html_tag($variables) {
  $element = $variables ['element'];
  $attributes = isset($element ['#attributes']) ? drupal_attributes($element ['#attributes']) : '';
  if (!isset($element ['#value'])) {
    return '<' . $element ['#tag'] . $attributes . " />\n";
  }
  else {
    $output = '<' . $element ['#tag'] . $attributes . '>';
    if (isset($element ['#value_prefix'])) {
      $output .= $element ['#value_prefix'];
    }
    $output .= $element ['#value'];
    if (isset($element ['#value_suffix'])) {
      $output .= $element ['#value_suffix'];
    }
    $output .= '</' . $element ['#tag'] . ">\n";
    return $output;
  }
}

Comments

<?php
 theme
('html_tag', array(
   
'element' => array(
     
'#tag' => 'h2',
     
'#attributes' => array(
       
'class' => 'activities-sidbar',
        ),
     
'#value' => 'Activity History',
    ),
  ));
?>

It seems that this won't support rendering children without extra help.
I'd expected :

<?php
      $test
= array(
       
'#type' => 'html_tag',
       
'#tag' => 'div',
       
'#attributes' => array('class' => 'outer'),
       
'0' => array(
         
'#type' => 'html_tag',
         
'#tag' => 'span',
         
'#attributes' => array('class' => 'inner'),
         
'#value' => 'Content',
        ),
      );
?>

to produce

<div class="outer">
  <span class="inner">
    Content
  </span>
</div>

but it fails.

Looks like you will have to call render() or drupal_render_children() on the child items yourself, and set the #value of your html_tag before allowing the normal render process to use it.

Could be that this desired behavior could happen with an alter of hook_preprocess_html_tag() as well... Though I think it's not asking too much for this low-level utility to behave as if it knows how to render HTML child elements.

Slightly old thread, but I wanted this functionality so wrote the necessary preprocess function. Might be of use to someone.

<?php
function THEMENAME_preprocess_html_tag(&$variables) {
 
// Loop over element children, render them, and add them to the #value string.
 
foreach (element_children($variables['element']) as $key) {
    if (
$variables['element']['#value'] === NULL) {
     
// Set this to string to avoid E_NOTICE error when concatenating to NULL.
     
$variables['element']['#value'] = '';
    }
   
// Concatenate the rendered child onto the element #value.
   
$variables['element']['#value'] .= drupal_render($variables['element'][$key]);
  }
}
?>

It would be easier to set the container type.

<?php
$render_array
= array(
 
'#type' => 'container',
 
'#attributes' => array(
   
'class' => array('my-class'),
  ),
 
'span-0' => array(
   
'#theme' => 'html_tag',
   
'#tag' => 'span',
   
'#value' => 'Stuff',
   
'#attributes' => array(
     
'class' => array('my-span-class'),
    ),
  ),
);
?>

If you use this function to create a script tag with an src attribute, note that it will be self-closing (and hence won't work) unless you provide a #value attribute and set it to empty string.

  $script_tag = array(
    '#tag' => 'script',
    '#attributes' => array(
      'type' => 'text/javascript',
      'src' => url('/path/to/js.js', array('absolute' => TRUE)),
    ),
    // We need to add an empty string for #value so the script tag will be
    // properly closed with </script>.
    '#value' => '',
    '#theme' => 'html_tag',
  );
  print render($script_tag);