function theme_html_tag

You are here

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 call to theme_html_tag()
ThemeHtmlTag::testThemeHtmlTag in modules/simpletest/tests/theme.test
Test function theme_html_tag()
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.

File

includes/theme.inc, line 2185
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'),
    ),
  ),
);
?>