Yii2.0 template references js and css files

Source: Internet
Author: User
Tags php file yii

Yii2 uses a more standard method to introduce js and css files through the AppAsset: register ($ this) method. In the Yii2 example, layouts main. php has its usage: AppAsset: register ($ this ).


There is an assets directory in the WEB directory, which contains an Appasset. Php file with the following content:

Namespace frontend \ assets;
 
Use yii \ web \ AssetBundle;
 
/**
* @ Author Qiang Xue <qiang.xue@gmail.com>
* @ Since 2.0
*/
Class AppAsset extends AssetBundle
{
Public $ basePath = '@ webroot ';
Public $ baseUrl = '@ web ';
Public $ css = [
'Css/site.css ',
];
Public $ js = [
];
Public $ depends = [
'Yii \ web \ yiiasset ',
'Yii \ bootstrap \ bootstrapasset ',
];
}

This AppAsset class inherits Yii \ web \ AssetBundle, which mainly defines the paths and dependencies of js and css files.

In the Template layout file main. php, use AppAsset: register ($ this) to register these css and js files. In addition, add the following in the html head:

<? Php $ this-> head ()?>

This statement generates a replacement character, indicating that the reference code of css and js is displayed here. Don't forget to add this sentence to the head.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.