search
Homephp教程PHP开发yii2.0 static resource js css introduction method

Configure resource bundle:

Yii2 uses the AssetBundle resource package class for CSS/JS management.

Open backend/assets/AppAsset.php

namespace backendassets; 
use yiiwebAssetBundle; 
class AppAsset extends AssetBundle { 
public $basePath = [email protected]/* */'; 
public $baseUrl = [email protected]/* */';
//全局CSS 
public $css = [ 
'css/animate.css', 
'css/style.min.css', 
]; 

//全局JS 
public $js = [ 
'js/jquery-2.1.1.js' 
]; 

//依赖关系 
public $depends = [ 
'yiiwebYiiAsset', 
'yiiootstrapBootstrapAsset', 
]; 

//定义按需加载JS方法,注意加载顺序在最后 
public static function addScript($view, $jsfile) { 
$view->registerJsFile($jsfile, [AppAsset::className(), 'depends' => 'backendassetsAppAsset']); 
} 

//定义按需加载css方法,注意加载顺序在最后 
public static function addCss($view, $cssfile) { 
$view->registerCssFile($cssfile, [AppAsset::className(), 'depends' => 'backendassetsAppAsset']); 
} 
 }

Static resources are loaded in the view

1. The view (or layout) uses global CSS/ JS

use yiihelpersHtml; 
use backendassetsAppAsset; 
use backendwidgetsAlert; 

AppAsset::register($this);

2. Load separate styles in the view

$cssString = ".gray-bg{color:red;}"; 
$this->registerCss($cssString);

3. Load separate JS

<?php$this->registerJs( 
&#39;$("document").ready(function(){ 
$("#login-form").validate({
errorElement : "small", 
errorClass : "error",
rules: {
"AgNav[nav_cn]": {
required: true,
},
},
messages:{
"AgNav[nav_cn]" : { 
required : "此字段不能为空.",
},
}
});
});&#39;
);?>

or

<div id="mybutton">点我弹出OK</div> 

<?php $this->beginBlock(&#39;test&#39;) ?> 
$(function($) { 
$(&#39;#mybutton&#39;).click(function() { 
alert(&#39;OK&#39;); 
}); 
}); 
<?php $this->endBlock() ?> <?php $this->registerJs($this->blocks[&#39;test&#39;], yiiwebView::POS_END); ?>

4. Introduce JS/CSS files into the view

Use custom functions

public static function addScript($view, $jsfile) { 
$view->registerJsFile($jsfile, [AppAsset::className(), &#39;depends&#39; => &#39;backendassetsAppAsset&#39;]); 
}
AppAsset::register($this); 
//只在该视图中使用非全局的jui 
AppAsset::addScript($this,[email protected]/* *//js/jquery-ui.custom.min.js&#39;); 
AppAsset::addCss($this,[email protected]/* *//css/font-awesome/css/font-awesome.min.css&#39;);

Load directly

AppAsset::register($this); 
//css定义一样 
$this->registerCssFile([email protected]/* *//css/font-awesome.min.css&#39;,[&#39;depends&#39;=>[&#39;backendassetsAppAsset&#39;]]); 
$this->registerJsFile([email protected]/* *//js/jquery-ui.custom.min.js&#39;,[&#39;depends&#39;=>[&#39;backendassetsAppAsset&#39;]]);

The above is the content of the yii2.0 static resource js css introduction method, more related Please pay attention to the PHP Chinese website (www.php.cn) for content!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.