Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Check if asset collection exists

Whats the correct way to check if an asset collection exists before attempting to output?

If, I have a "footer" collection and attempt to output in my main layout without adding assets it throws an exception. If I use $this->assets->get('footer'), it will throw an exception if not created. This is what i do for now, but seems wrong:

echo $this->assets->outputJs('footer');


this way:

    echo $this->assets->outputJs('footer');


Thanks! I didnt see the count method. That makes more sense semantically, even though calling collection('footer') implicitly creates a collection I think.

Just an interesting question: I added 3 JS files to the collection

// IndexController  indexAction

but when i did

// views/index.volt
{{ assets.collection("footer").count() }}


// views/index.phtml
echo $this->assets->collection("footer")->count();

the result is always 2 ?!

Am I missing something or it is a bug?



It is not a bug. You probably don't have one of the files added with addJs on the filesystem.

edited May '16


Thanks for the help. I also thought that, but it is not the situation.

Actually, i realized that i was watching the wrong assets collection. In the problematic collection i have 2 addJs calls and 1 addInlineJs call. In my opinion count() should return 3 while it really returns 2?!

I'd appreciate help here. Thanks



If you look at

You will see that the count() method returns $this->_resources:

return count(this->_resources);

which is populated only by addCss and addJs methods.

addInlineJs and addInlineCss use this->_codes which is not returned by a count method.

So, yes, correct answer is 2 (as inline isn't counted by the count method).