Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include CSS,javascript file in Yii Framework

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.

like image 587
viji Avatar asked Jan 04 '10 09:01

viji


People also ask

How to register css file in Yii2?

Registering CSS files A CSS file can be registered using the following: $this->registerCssFile("@web/css/themes/black-and-white. css", [ 'depends' => [\yii\bootstrap\BootstrapAsset::class], 'media' => 'print', ], 'css-print-theme'); The above code will add a link to the /css/themes/black-and-white.

What is module in yii?

A module is a self-contained software unit that consists of models, views, controllers and other supporting components. In many aspects, a module resembles to an application. The main difference is that a module cannot be deployed alone and it must reside inside of an application.

What is controller in yii?

Controllers are part of the MVC architecture. They are objects of classes extending from yii\base\Controller and are responsible for processing requests and generating responses.

What is namespace yii?

A namespace refers to a logical grouping of some class names so that they can be differentiated from other class names even if their names are the same. Do not confuse path alias with namespace. A path alias is merely a convenient way of naming a file or directory.


2 Answers

Something like this:

<?php     $baseUrl = Yii::app()->baseUrl;    $cs = Yii::app()->getClientScript();   $cs->registerScriptFile($baseUrl.'/js/yourscript.js');   $cs->registerCssFile($baseUrl.'/css/yourcss.css'); ?> 
like image 178
Alexander Hramov Avatar answered Sep 18 '22 22:09

Alexander Hramov


You can do so by adding

Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.'/path/to/your/script'); 
like image 32
ysing Avatar answered Sep 17 '22 22:09

ysing