function drupal_pre_render_conditional_comments

You are here

7 common.inc drupal_pre_render_conditional_comments($elements)
8 common.inc drupal_pre_render_conditional_comments($elements)

#pre_render callback to render #browsers into #prefix and #suffix.

Parameters

$elements: A render array with a '#browsers' property. The '#browsers' property can contain any or all of the following keys:

  • 'IE': If FALSE, the element is not rendered by Internet Explorer. If TRUE, the element is rendered by Internet Explorer. Can also be a string containing an expression for Internet Explorer to evaluate as part of a conditional comment. For example, this can be set to 'lt IE 7' for the element to be rendered in Internet Explorer 6, but not in Internet Explorer 7 or higher. Defaults to TRUE.
  • '!IE': If FALSE, the element is not rendered by browsers other than Internet Explorer. If TRUE, the element is rendered by those browsers. Defaults to TRUE.

Examples:

  • To render an element in all browsers, '#browsers' can be left out or set to array('IE' => TRUE, '!IE' => TRUE).
  • To render an element in Internet Explorer only, '#browsers' can be set to array('!IE' => FALSE).
  • To render an element in Internet Explorer 6 only, '#browsers' can be set to array('IE' => 'lt IE 7', '!IE' => FALSE).
  • To render an element in Internet Explorer 8 and higher and in all other browsers, '#browsers' can be set to array('IE' => 'gte IE 8').

Return value

The passed-in element with markup for conditional comments potentially added to '#prefix' and '#suffix'.

1 string reference to 'drupal_pre_render_conditional_comments'
system_element_info in modules/system/system.module
Implements hook_element_info().

File

includes/common.inc, line 5504
Common functions that many Drupal modules will need to reference.

Code

function drupal_pre_render_conditional_comments($elements) {
  $browsers = isset($elements['#browsers']) ? $elements['#browsers'] : array();
  $browsers += array(
    'IE' => TRUE,
    '!IE' => TRUE,
  );

  // If rendering in all browsers, no need for conditional comments.
  if ($browsers['IE'] === TRUE && $browsers['!IE']) {
    return $elements;
  }

  // Determine the conditional comment expression for Internet Explorer to
  // evaluate.
  if ($browsers['IE'] === TRUE) {
    $expression = 'IE';
  }
  elseif ($browsers['IE'] === FALSE) {
    $expression = '!IE';
  }
  else {
    $expression = $browsers['IE'];
  }

  // Wrap the element's potentially existing #prefix and #suffix properties with
  // conditional comment markup. The conditional comment expression is evaluated
  // by Internet Explorer only. To control the rendering by other browsers,
  // either the "downlevel-hidden" or "downlevel-revealed" technique must be
  // used. See http://en.wikipedia.org/wiki/Conditional_comment for details.
  $elements += array(
    '#prefix' => '',
    '#suffix' => '',
  );
  if (!$browsers['!IE']) {
    // "downlevel-hidden".
    $elements['#prefix'] = "\n<!--[if $expression]>\n" . $elements['#prefix'];
    $elements['#suffix'] .= "<![endif]-->\n";
  }
  else {
    // "downlevel-revealed".
    $elements['#prefix'] = "\n<!--[if $expression]><!-->\n" . $elements['#prefix'];
    $elements['#suffix'] .= "<!--<![endif]-->\n";
  }

  return $elements;
}

Comments

any examples of how to use this? im trying to wrap conditional comments for a js script without doing it through the html.tpl file.

<?php
function MYTHEME_preprocess_html(&$vars) {
 
// Add conditional stylesheet for IE
 
drupal_add_css(path_to_theme() . '/css/ie.css',
    array(
     
'group' => CSS_THEME,
     
'browsers' =>
      array(
       
'IE' => 'lte IE 8', // This targets IE less than or equal to (lte) 8
       
'!IE' => FALSE),
     
'preprocess' => FALSE));
?>