Yii2: Asset handling for a custom theme

I created a theme, its working file, but the only problem is the data file from the theme .

I am creating a theme in the following path



And my theme file structure



I put all css, js path in assets/AppAssets.php

, so mine AppAssets.php

looks like

namespace app\assets;

use yii\web\AssetBundle;

 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
class AppAsset extends AssetBundle
    public $sourcePath = '@app/themes/bluehorse';
    public $css = [

    public $js = [
    public $depends = [


And my layout (for example main.php

) looks like

use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\widgets\Breadcrumbs;
use app\assets\AppAsset;




You can see what I am using AppAssets

as assets for my themes. But I don't want to use AppAssets

. I want to create my own resources in my theme. I want to put all the css, js path in my won assets

file to be placed in my theme folder. Maybe?? I need help.


source to share

2 answers

You can put your own objects in the theme (exactly in the field of view):

/* @var $this yii\web\View */
    $this->registerJsFile('@themes/theme_name/js/name.js', ['depends' => 'yii\web\YiiAsset']) ;
    $this->registerCssFile('@themes/theme_name/css/name.css' ) ;


can change the layout in the controller like:

public $layout = 'another_name_layout';


There are many ways to provide change topics, one of which is:

if ($layout = 'another_name_layout') {
$this->registerCssFile('@web/css/name.css' ) ;




you can insert your assets (css and js) into directories. you have to create an asset file like appAssets add your own css and js. be careful: "UrlPath" and "BasePath" should be your subject directory.

Mark!!! you have to set the following in your main layout:

<?php $this->beginPage() ?>
<?php $this->beginBody() ?>
<?php $this->head() ?>
<?php $this->endBody() ?>
<?php $this->endPage() ?>




All Articles