7.x 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

2 theme calls to theme_html_tag()
drupal_get_js in includes/common.inc
Returns a themed presentation of all JavaScript code for the current page.
system_element_info in modules/system/system.module
Implements hook_element_info().

File

includes/theme.inc, line 2278
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

jibran’s picture

 theme('html_tag', array(
    'element' => array(
      '#tag' => 'h2',
      '#attributes' => array(
        'class' => 'activities-sidbar',
        ),
      '#value' => 'Activity History',
    ),
  ));
dman’s picture

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

      $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.

kpa’s picture

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

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]);
  }
}
mradcliffe’s picture

It would be easier to set the container type.

$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'),
    ),
  ),
);
donquixote’s picture

But then it will be div, not a custom tag name. I want to have it be ..

stevenc’s picture

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

i.e. you need an array of arrays

rohnjeynolds’s picture

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' => '',
    '#type' => 'html_tag',
  );
  print render($script_tag);
josephdietrich’s picture

The same is true of other HTML elements such as iframe that do not allow end tag omission, at least up to Drupal version 7.51.

roynilanjan’s picture

In this working to follow the conventional approach according to theme_html_tag rather to follow https://api.drupal.org/comment/61491#comment-61491, as this require to render the array through drender/drupal_render

theme('html_tag', array(
    'element' => array(
      '#tag' => 'div',
      '#attributes' => array(
        'class' => 'activities-sidbar',
        ),
      'children' => array(
        'element' => array(
          '#tag' => 'span',
          '#attributes' => array(
            'class' => 'inner'
           ),
          '#value' => 'Content',   
        ),
      ),
    ),
  ));