CclientScript::POS_HEAD and real position

Hello,

I try to add somme JS and CSS in my application using CclientScript :




<!DOCTYPE html>

<html lang="<?php echo Yii::app()->language?>">

<head>

    <?php //Add meta tags

    echo CHtml::tag('meta', array('charset'=>Yii::app()->charset))?>


   <?php //Include necessary global CSS and JS files

    Yii::app()->clientScript->registerCssFile(Yii::app()->params->cssUrl.'bootstrap.min.css');

    Yii::app()->clientScript->registerCssFile(Yii::app()->params->cssUrl.'styles.css');

    Yii::app()->clientScript->registerScriptFile(Yii::app()->params->jsUrl.'bootstrap.min.js'); ?>


    <title><?php echo Yii::t('common', 'application.name').' | '.$this->pageTitle; ?></title>

</head>


<body>

<header>

    <?php $this->widget('UserMenu'); ?>

</header>


<div class="container">

    <section><?php echo $content ?></section>

    <footer></footer>

</div>

</body>

</html>



But when i look to the source code I see that the Css and JS links are placed in the body instead of the heads :





<!DOCTYPE html>

<html lang="fr">

<head>

    <meta charset="UTF-8" />

    

    <title>PLOP | view.create.title</title>

</head>


<body>

<header>

    <link rel="stylesheet" type="text/css" href="/assets/usa/assets/style-67f1b824.css?1333060883" />

<script type="text/javascript" src="/assets/usa/assets/script-0-42220737.js?1333060883"></script>


<div class="navbar">

    <div class="navbar-inner">

        <div class="container">

            <a class="brand" href="#">

                PLOP            </a>

            <ul class="nav">

                <li><a href="#">News</a></li>

                <li><a href="#">Photo</a></li>

            </ul>

            <li><button id="login" class="btn btn-small btn-primary pull-right">application.template.login</button></li>

        </div>

    </div>

</div>


</header>


<div class="container">

<section></section>

    <footer></footer>

</div>


</script>

</body>

</html>



The CSS and JS scripts should be places before the title tag, right ? (I tried without the header tag)

Also an other question, I use CclientScript to add some javascript code. It works great. But how can I use the CclientScript->registerScript() to write the javascript multilines ? Let say, I have a very long script, I don’t want to write it on a single line inside the PHP function.

Thanks,

Maxime.

Ok I found the solution.

The portlet I render in this view has the run function overide.

To not use the


$content=ob_get_clean();

is somehow a bas idea.

Still can I get an answer for this ?

I use CclientScript to add some javascript code. It works great. But how can I use the CclientScript->registerScript() to write the javascript multilines ? Let say, I have a very long script, I don’t want to write it on a single line inside the PHP function.

Thanks,

Maxime.

Hi. Well I am not able to answer the first part.

For the second one:

At least two possibilities:

[list=1]

[*]straightforward:


$script = <<<HTML

    /* here you write your javascript normally in multiple lines */

HTML;

Yii::app()->clientScript->registerScript('someId', $script, CClientScript::POS_READY); // POS_READY is an example, please check the docs

[*]less elegant, but works as well:


Yii::app()->clientScript->registerScript("someId", "

    /* here you write your javascript normally in multiple lines

        but please watch out the quotes & single quotes */

", CClientScript::POS_READY); // POS_READY is an example, please check the docs

[/list]

Hello,

Thanks for the reply :)

I tried the heredoc syntax, but I couldn’t add a call to a Yii static function. Is it possible to add something like Yii::t() in heredoc ?

Thanks,

Maxime.

Could be because you’ve put the client script code in a view file.

Try putting it in your controller/action.

I’ll try, but I don’t understand why put the CclientScript code would change something ?

My problem is not about a php error but how to write the js script on multiple lines inside the CclientScript->registerScript() method.

Maxime.

Hi.

Either I misunderstood or I wasn’t clear enough.

With the no. 2, yes, you just do normal PHP string ops. Example:


Yii::app()->clientScript->registerScript("someId", "

    $(document).ready(function(){

        $('body').prepend('<div class=\"flash-notice\">" . Yii::t(app, "Startup_Message") . "<div>').fadeIn();

    });

", CClientScript::POS_END);

Yes , I understand it is posible with a double quotes but he Hereroc system (<<<EOD EOD) doesn’t allow to call a function like Yii::t(), yes ?

Thanks,

Maxime.

If your concern is heredoc, you could check heredoc syntax and limitations: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc