How to include a Javascript or CSS file in Yii Framework?
I want to create a page on my site that has a little Javascript application running, so I want to include .js and .css files in a specific view.
How to include a Javascript or CSS file in Yii Framework?
I want to create a page on my site that has a little Javascript application running, so I want to include .js and .css files in a specific view.
Something like this:
<?php
$baseUrl = Yii::app()->baseUrl;
$cs = Yii::app()->getClientScript();
$cs->registerScriptFile($baseUrl.'/js/yourscript.js');
$cs->registerCssFile($baseUrl.'/css/yourcss.css');
?>
You can do so by adding
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.'/path/to/your/script');
If you are using Theme then you can the below Syntax
Yii::app()->theme->baseUrl
include CSS File :
<link href="<?php echo Yii::app()->theme->baseUrl;?>/css/bootstrap.css" type="text/css" rel="stylesheet" media="all">
Include JS File
<script src="<?php echo Yii::app()->theme->baseUrl;?>/js/jquery-2.2.3.min.js"></script>
If you are not using theme
Yii::app()->request->baseUrl
Use Like this
<link href="<?php echo Yii::app()->request->baseUrl; ?>/css/bootstrap.css" type="text/css" rel="stylesheet" media="all">
<script src="<?php echo Yii::app()->request->baseUrl; ?>/js/jquery-2.2.3.min.js"></script>