dqroktbn005028 2014-08-18 02:38
浏览 25

Yii框架:在控制器定义的布局中加载css和js?

My target is include difference css (or js )for difference page. And the code for link css is locate at the layout file.

My first idea is pass variable to layout, like the way you pass variable to view, but I found that it is "can not do that by design" from Yii framework forum.

So How will you to do for putting <link rel="stylesheet" ... > in the header (which inside layout file) ?

Please correct me if my concept is wrong.

  • 写回答

1条回答 默认 最新

  • dpmwy80068 2014-08-18 02:41
    关注

    You will want to go to your actions, and register the CSS file for them. Example:

    function actionIndex() {
        $cs = Yii::app()->getClientScript();
        $cs->registerCssFile(Yii::app()->getBaseUrl() . '/css/myStyle.css');
        $cs->registerScriptFile(Yii::app()->getBaseUrl() . '/js/myScript.js');
    
        // more stuff
    }
    

    You can also pass the script loader (not CSS) an extra parameter to decide where it goes, like so:

    // load on document ready
    $cs->registerScriptFile(Yii::app()->getBaseUrl() . '/js/myScript.js', CClient::POS_READY); 
    

    See the documentation for further help.

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)