We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon Asset Management with Optional Page outputJS and OutputCss ?

In my website i have some pages with jquery lightbox and some pages without lightbox, to load the pacakges i use following in my controller function which seems to work fine.

public function loadJSLightbox2()
{
    $this->assets
        ->collection('lightboxJs')
        //->setPrefix($this->config->site->cloud_url)
        ->addJs('web/dist/lightbox2/dist/js/lightbox.min.js');
    $this->assets
        ->collection('lightboxCss')
        //->setPrefix($this->config->site->cloud_url)
        ->addCss('web/dist/lightbox2/dist/lightbox.min.css');
}

However in my volt main template (master template) i have define the place holders like below,

 {{ assets.outputJs('lightboxJs') }}
 {{ assets.outputCss('lightboxCss') }}

this works fine as long we this loadJSlightbox2 functions is called when loading the page. In pages where i dont need to load the plugin i get error called as below.

Phalcon\Assets\Exception: The collection does not exist in the manager

How do i archieve this behaviour in my web applicaiton ?

edited Mar '16

How about:

{% if assets.get('lightboxJs') %}
 {{ assets.outputJs('lightboxJs') }}
 {{ assets.outputCss('lightboxCss') }}
 {% endif %}

Oh nvm - its not gonna work. There is function getCollections - which will return collections, as an array where key is collection name, just check if there exists your collection. I will do PR with exists function today later cuz it's doesnt exist, so you dont have to get all collections.



12.4k

I asked a similar question. Jurigag try to ask again (https://github.com/phalcon/cphalcon/issues/11408)



13.4k

how do i solve this problem temp will following work ?

{% for collection in assets.getCollections('lightboxCss') %}
    {% if collection.lightboxCss !="" %}
        {{ assets.outputCss('lightboxCss') }}
    {% endif %}
{% endfor %}

No no, more likely:

{% if assets.getCollections()['lightboxCss'] %}

{% endif%}

But it looks bad, not sure its even working in volt, that's why i added just simple exists in PR https://github.com/phalcon/cphalcon/pull/11558 If you need it right now you can clone my branch and compile it.



13.4k

finally i fixed it using following code

{% for key, collection in assets.getCollections() %}
    {% if key == 'origin' %}
        {{ assets.outputJs('origin') }}
    {% endif %}
    {% if key == 'lightboxJs' %}
        {{ assets.outputJs('lightboxJs') }}
    {% endif %}
    {% if key =='colorboxJs' %}
        {{ assets.outputJs('colorboxJs') }}
    {% endif %}
{% endfor %}
edited Mar '16

Well i guess exists method would still be helpful. At least we wouldnt need to get all collections.



13.4k

as far as i am concern when you call this functions like this

 assets.getCollections('lightboxCss')

returning all collections is wrong and misleading, it should be empty or return false on that case. it should be a bug.

No, its throwing exception beacause collection doesnt exist and i think it's good behaviour. That's why i added exists function to check if collection exists. Then for example other programmer when he will see okay assets.getCollection('lightboxCss') but why there is nothing in html from it He will ask himself what's going on ? It's not a bug at all, throwing exception it's good thing, beacause it doesn't exists. Returning empty collection or false it's bad behaviour imho.

edited Mar '16

Now in branch 2.1.x you can use exists method to check if colection exists instead of getting all collections.