Assets ====== > Note: This section is under writing. An asset in Yii is a file that may be referenced or linked in a Web page. It can be a CSS file, a JavaScript file, an image or video file, etc. For simple Web applications, assets may be managed manually - you place them in a Web folder and reference them using their URLs in your Web pages. However, if an application is complicated, or if it uses many third-party extensions, manual management of assets can soon become a headache. For example, how will you ensure one JavaScript file is always included before another and the same JavaScript file is not included twice? How will you handle asset files required by an extension which you do not want to dig into its internals? How will you combine and compress multiple CSS/JavaScript files into a single one when you deploy the application to production? In this section, we will describe the asset management capability offered by Yii to help you alleviate all these problems. ## Asset Bundles Assets are organized in *bundles*. An asset bundle represents a collection of asset files located under a single directory. It lists which CSS and JavaScript files are in this collection and should be included in a page where the bundle is used. ### Defining Asset Bundles An asset bundle is defined in terms of a PHP class extending from [[yii\web\AssetBundle]]. In this class, you use certain class properties to specify where the asset files are located, what CSS/JavaScript files the bundle contains, and so on. The class should be namespaced and autoloadable. Its name is used as the name of the asset bundle. The following code defines the main asset bundle used by [the basic application template](start-installation.md): ```php Assets, based on their location, can be classified as: * source assets: the asset files are located together with PHP source code which cannot be directly accessed via Web. In order for source assets to be Web accessible, they should be published and turned in *published assets*. * published assets: the asset files are located in a Web folder and can thus be directly accessed via Web. * external assets: the asset files are located on a Web server that is different from the one hosting your Web application. For assets that directly belong to an application, it is recommended that you place them in a Web folder to avoid the unnecessary asset publishing process. This is why `AppAsset` specifies [[yii\web\AssetBundle::basePath|basePath]] without [[yii\web\AssetBundle::sourcePath|sourcePath]]. For assets belonging to an [extension](structure-extensions.md), as they are in a folder that is not Web accessible, you have to specify the [[yii\web\AssetBundle::sourcePath|sourcePath]] property when declaring the corresponding asset bundle. > Note: Do not use `@webroot/assets` as the [[yii\web\AssetBundle::sourcePath|source path]]. This folder is used by default by the [[yii\web\AssetManager|asset manager]] to keep the asset files published from their source location. Any content in this folder are considered temporarily and may be subject to removal. #### Asset Dependencies When you include multiple CSS or JavaScript files on a Web page, they have to follow certain orders to avoid unexpected overriding. For example, if you are using a jQuery UI widget in a Web page, you have to make sure the jQuery JavaScript file is included before the jQuery UI JavaScript file is included. We call such ordering the dependencies among assets. Asset dependencies are mainly specified through the [[yii\web\AssetBundle::depends]] property of asset bundles. In the `AppAsset` example, the asset bundle depends on two other asset bundles: `yii\web\YiiAsset` and `yii\bootstrap\BootstrapAsset`. And for the jQuery UI example just described, the asset bundle is declared as follows: ```php class JuiAsset extends AssetBundle { public $sourcePath = '@bower/jquery-ui'; public $js = [ 'jquery-ui.js', ]; public $css = [ 'themes/smoothness/jquery-ui.css', ]; public $depends = [ 'yii\web\JqueryAsset', ]; } ``` ## Using Asset Bundles To use an asset bundle, register it with a [view](structure-views.md) like the following in a view template: ```php use app\assets\AppAsset; AppAsset::register($this); ``` where `$this` refers to the [[yii\web\View|view]] object. If you are registering an asset bundle in a PHP class, you should provide the needed the view object. For example, to register an asset bundle in a [widget](structure-widgets.md) class, you can obtain the view object by `$this->view`. When an asset bundle is registered, behind the scene Yii will register all its dependent asset bundles. And when a page is being rendered, `` and `