在Yii中的<head>来自控制器中显示自定义HTML

We are using Yii for our project. I am trying to conditionally register two JS scripts/assets in the header: one for IE8 and one for other browsers - using conditional comments (e.g. <!--[if lte IE 8]>).

However, I am only familiar with Yii::app()->clientScript->registerScriptFile and Yii::app()->clientScript->registerScript, none of which exposes a way to surround the registered script with a conditional comment.

I tried directly doing echo at the start of the controller action:

echo '<!--[if lte IE 8]><script src="'.$assetsPath . '/charts/r2d3.js'.'" charset="utf-8"></script><![endif]-->';

However, when I look in the source the script is displayed at the top of the document (before even <html>). If possible, I'd like to display it in <head>.

You could use the following extension

http://www.yiiframework.com/extension/ewebbrowser/

I put the file in component. Then just do in the code

$b = new EWebBrowser();
if($b->platform == "Internet Explorer" && $b->version == "8.0") {//you could explode version at the . I guess, however not sure if there is even a version 8.x or whatever
    Yii::app()->clientScript->registerScriptFile("path/to/script");
} else {
    Yii::app()->clientScript->registerScriptFile("path/to/otherscript");
}

I did test this with the current IE, Firefox and Chrome browser. I can not ensure this would work with other version of these browser. It two years old, but it still seems to be working.

You can set this in head as following,

Go to ->views->layouts->main.php

Simple add what ever the content you need after,

i.e

<!DOCTYPE html>
[if lt IE 7]><html class="no-js lt-ie9 lt-ie8 lt-ie7"><![endif]
[if IE 7]><html class="no-js lt-ie9 lt-ie8"> <![endif]
[if IE 8]><html class="no-js lt-ie9"> <![endif]
[if gt IE 8]><html class="no-js"> <![endif]
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
</head>

or if you need to check a browser version, in a controller and append scripts. this way would also work.

define this in controller,

public function beforeRender( $view ) {
//condition to check the browser type and version
if($browser = 'ie' && $version = '8') {
    Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/js/script.js');
    return true;
}
}

// havent tested the second option. but should work.

you can check the browser type and version with any of the following,

<?php
echo $_SERVER['HTTP_USER_AGENT'];

$browser = get_browser(null, true);
print_r($browser);
?>

check this link, http://php.net/manual/en/function.get-browser.php#101125

This is not the optimum method, but you can achieve you goal using following code:

If you want above conditional statement for particular Controller, then:

Under you layouts/main.php

<?php if (Yii::app()->getController()->getId() == 'Your Controller Name'): ?>
<!--[if lt IE 8]>
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/ie.css" media="screen, projection" />
<![endif]-->
<?php endif; ?>

If you want above conditional statement for Action of particular Controller, then:

Under you layouts/main.php

<?php if (Yii::app()->getController()->getId() == 'Your Controller Name' && Yii::app()->getController()->getAction()->getId() == 'Your Action Name'): ?>
<!--[if lt IE 8]>
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/ie.css" media="screen, projection" />
<![endif]-->
<?php endif; ?>