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

/var/www/html/project_name/themes/theme_name

      

And my theme file structure

<theme_name>
    -layouts
        main.php
    -css
        style.css
    -js
        script.js
    -images
        logo.jpg

      

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 = [
        'css/style.css'

    ];
    public $js = [
        script.js
    ];
    public $depends = [
       'yii\web\YiiAsset',
       'yii\bootstrap\BootstrapAsset',
    ];
}

      

And my layout (for example main.php

) looks like

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

AppAsset::register($this);
...................



?>

      

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.

+3


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' ) ;
}

      

+1


source


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() ?>

      

+1


source







All Articles