Yii Framework Forum: Assetbundle Position On Page - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Assetbundle Position On Page

#1 User is offline   Arman Poghosyan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 24-March 12

Posted 13 February 2014 - 06:54 AM

While working on new project with Yii2, I've decided to use new AssetBundle to leverage it's compressing and converting functionality later on. It's great addition to Yii. The concept of asset bundles on it's own is great even without compressing and converting, but...

Unfortunately I've noticed that there is no any way to define where the asset bundle should be inserted on page. View::registerJs has second argument, where you can define position on page. AssetBundle class uses the same function to register all js assets with default/empty argument (meaning that assets are inserted in head section). But jQuery for example can (usually must) be loaded in right before closing body tag. And it is good practice to load jQuery and other heavy libraries there, so that the page is loaded faster (content of the page).

Won't it be reasonable to add an option to asset bundle class to define where to load js scripts?
Do I need to open issue or even apply patch for it?

(I just wanted to listen for an opinion of other devs)
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,225
  • Joined: 17-January 09
  • Location:Russia

Posted 13 February 2014 - 08:39 AM

Default isn't head. It's end of body.

Also you can't specify position manually since that will override dependencies.
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I can work more on Yii: https://www.patreon.com/samdark
0

#3 User is offline   Kartik V 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 652
  • Joined: 29-August 12

Posted 13 February 2014 - 09:55 AM

View PostArman Poghosyan, on 13 February 2014 - 06:54 AM, said:

Unfortunately I've noticed that there is no any way to define where the asset bundle should be inserted on page.

Yes I think its for dependency management. If you can ensure dependencies are ok ... you could still achieve what you want to some extent by using multiple bundles and split your js files between the bundles.

You can control the jsOptions in the bundle, to control the positioning of the js files for the bundle. I have not tested this, but this should theoretically work. For example:

class MyBeginBundle extends AssetBundle {
    public function init() {
        $this->jsOptions['position'] = View::POS_BEGIN;
        parent::init();
    }
}

class MyEndBundle extends AssetBundle {
    public function init() {
        $this->jsOptions['position'] = View::POS_END;
        parent::init();
    }
}

In your view or your layout file:

MyBeginBundle::register();
MyEndBundle::register();

0

#4 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,907
  • Joined: 04-October 08
  • Location:DC, USA

Posted 13 February 2014 - 03:10 PM

Yes, you should use "jsOptions" to control the insertion position of the js files in your bundle. The options will be applied to ALL js files in the same bundle. This is so because the potion itself indicates some dependency and we don't want it to mess up with the bundle dependency.
0

#5 User is offline   Arman Poghosyan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 24-March 12

Posted 13 February 2014 - 04:25 PM

@samdark thanks.

Quote

Default isn't head. It's end of body.

Sorry, I've just noticed that they are in the end of body.

@Kartik, @Qiang thanks for your answers. OK, now I see how I can do that. It's great thanks.

I simply was looking on registerJs function in View and thought that the same function was used in AssetBundle internally. But it turned out that AssetBundle uses registerJsFile and position is the 3rd argument, not 2nd as in registerJs.
0

#6 User is offline   CTala 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 111
  • Joined: 17-August 11
  • Location:Chile

Posted 21 April 2014 - 10:23 AM

View PostKartik V, on 13 February 2014 - 09:55 AM, said:

class MyEndBundle extends AssetBundle {
    public function init() {
        $this->jsOptions['position'] = View::POS_END;
        parent::init();
    }
}




I tried to do this, it seems that it was what I was looking for. But I am having the following errror :

"Class 'app\assets\View' not found" and the error is when I do this -> "$this->jsOptions['position'] = View::POS_END;"

I understand that now "$this" refers to the view, but how I can access the local parameter then ? ( I tried using $jsOptions['position'] without luck neither ).
--
MSc. Cristian Tala Sánchez
0

#7 User is offline   Kartik V 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 652
  • Joined: 29-August 12

Posted 21 April 2014 - 01:18 PM

View PostCTala, on 21 April 2014 - 10:23 AM, said:

I tried to do this, it seems that it was what I was looking for. But I am having the following errror :

"Class 'app\assets\View' not found" and the error is when I do this -> "$this->jsOptions['position'] = View::POS_END;"

I understand that now "$this" refers to the view, but how I can access the local parameter then ? ( I tried using $jsOptions['position'] without luck neither ).

Remember that namespaces are very important in Yii 2.0. You must include this in the beginning of your code for the View class to be identified:
use yii\web\View;

0

#8 User is offline   CTala 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 111
  • Joined: 17-August 11
  • Location:Chile

Posted 21 April 2014 - 02:20 PM

View PostKartik V, on 21 April 2014 - 01:18 PM, said:

Remember that namespaces are very important in Yii 2.0. You must include this in the beginning of your code for the View class to be identified:
use yii\web\View;




Thanks ! I am still trying to get use to namespaces. :).
--
MSc. Cristian Tala Sánchez
0

#9 User is offline   jimmypage 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 17-June 14

Posted 24 June 2014 - 10:01 PM

View PostKartik V, on 13 February 2014 - 09:55 AM, said:

Yes I think its for dependency management. If you can ensure dependencies are ok ... you could still achieve what you want to some extent by using multiple bundles and split your js files between the bundles.

You can control the jsOptions in the bundle, to control the positioning of the js files for the bundle. I have not tested this, but this should theoretically work. For example:

class MyBeginBundle extends AssetBundle {
    public function init() {
        $this->jsOptions['position'] = View::POS_BEGIN;
        parent::init();
    }
}

class MyEndBundle extends AssetBundle {
    public function init() {
        $this->jsOptions['position'] = View::POS_END;
        parent::init();
    }
}

In your view or your layout file:

MyBeginBundle::register();
MyEndBundle::register();



Very nice, its work for me!
Thank you
Just a noober
0

#10 User is offline   frops 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 07-June 13

Posted 16 July 2015 - 06:24 AM

Good day!
As I understand it, the $JsOption['position'] used for all files JS.
What should I do if I want to use a different JS for different positions.
For example, I have js_for_head.js and js_for_end.js.
First file I want to place in the head, and the second at the end.
Thank you.
0

#11 User is offline   hjb 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 15-November 11
  • Location:Manchester, UK

Posted 12 July 2018 - 06:57 AM

Dragging this old thread up as there are very few mentions that this has since changed. Even the documentation doesn't mention it or show an example, but the ability to add options at the file level is now available in asset bundles.

For example:

['path/to/js-file.js', 'position' => \yii\web\View::POS_HEAD]


Hopefully that's useful to people, including future me.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users