Yii2 Framework de harici olarak CSS ve JS dosyaları eklenebildiği gibi, sayfa içinde nereye eklenecekleri de aşağıda belirtilmiştir.
$asset=backend\assets\AppAsset::register($this);
$baseUrl=$asset->baseUrl;
$this->registerCssFile($baseUrl."/path/to/file.css",[
'depends' => [\yii\bootstrap\BootstrapAsset::className()],
'media' => 'print',
], 'css-print-theme');
$this->registerJsFile($baseUrl.'/path/to/file.js',[
'depends' => [\yii\web\JqueryAsset::className()],
'position' => \yii\web\View::POS_END
]);
View::POS_HEAD
for head section.View::POS_BEGIN
for right after opening .View::POS_END
for right before closing .View::POS_READY
for executing code on document ready event.View::POS_LOAD
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
//View::POS_HEAD buraya eklenir
</head>
<body>
//View::POS_BEGIN buraya eklenir
<h1>My First Heading</h1>
<p>My first paragraph.</p>
//View::POS_END buraya eklenir
//View::POS_READY buraya eklenir
//View::POS_LOAD buraya eklenir
</body>
</html>
Inline Javascript Eklemek
<?php
$this->registerJs(
"$('#myButton').on('click', function() { alert('Button clicked!'); });",
yii\web\View::POS_READY,
'my-button-handler'
);
?>
inline CSS Eklemek
<?php
$this->registerCss("body { background: #f00; }");
?>