A
A
AJ2015-03-27 17:29:42
Yii
AJ, 2015-03-27 17:29:42

Yii2 + Twig. Do I need to use AssetBundle?

All the best. I opened this miracle framework. Everything is smart, convenient and fast. Raised the first site and realized that using a custom approach with php templates is not comme il faut. It’s hard for the layout designer, it hurts the eye, etc.
I screwed Twig, Bower and started rendering all this stuff. And ran into AssetsBundle. On the one hand, this is very convenient for me, as a back-end developer, on the other hand, it twitches every time because the front-end requires you to connect a slider or something else quite expensive. It's much easier to outsource the free insertion of scripts/styles directly into Twig templates.
In connection with this, a question arose. Which approach is relevant in terms of Best Practice in Yii2?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LAV45, 2015-03-30
@LAV45

namespace frontend\assets;

class AppAsset extends AssetBundle
{
    public $sourcePath = '@frontend/resource';

    public $css = [
        'css/site.css',
    ];

    public $depends = [
        'yii\web\JqueryAsset',
    ];
}

namespace frontend\widgets\FlexSlider;

class SliderAsset extends AssetBundle
{
    public $sourcePath = '@vendor/bower/flexslider';

    public $css = [
//        'flexslider.css'
    ];

    public $js = [
        'jquery.flexslider.js'
    ];

    public $depends = [
        'frontend\assets\AppAsset',
        'yii\web\JqueryAsset',
    ];
}

There is no need to shove all css and js scripts into one AssetBundle
In this example, AppAsset is the base style, when registered on the page, site.css and jQuery will be added, and the SliderAsset scripts will be connected immediately after the AppAsset scripts.
And it's okay if you call several AssetBundle on the page at once, js and css scripts will be added only once. In $depends we describe what may be needed for a particular AssetBundle to work. For example, SliderAsset needs JqueryAsset and styles that are described in AppAsset.
More details can be found here https://github.com/yiisoft/yii2/blob/master/docs/g...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question