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

Add assets in Volt

Is it possible to add an asset from a Volt template? Something like:

{% assets.addJs('js/filename.js') %}

Or do I need to do it with PHP code?

edited Oct '14

Check the docs for more info -> https://github.com/phalcon/docs/blob/master/en/reference/assets.rst In your controller:

$this->assets->collection('header')->addJs('js/jquery.js')->addJs('js/bootstrap.min.js');

And in your volt template:

{{ this.assets.outputJs('header') }}

This should work



32.2k

No guys, I don't want to output tags! What I need is to add a file to assets from a template. I can do that with the following PHP code both from the controller and from the template, but so far I couldn't find a way to use Volt. The case for that would be if a front-end developer or designer needs to include a JavaScript file for that particular view. It would be nice to have something in Volt that would allow for that.

$this->assets->addJs('js/filename.js')

I'm looking for the equivalent Volt code of the previous PHP code ;)



19.2k
Accepted
answer

That will do the job ;)

{% do assets.addJs('js/filename.js') %}

@chebureque, Thanks to my problem was resolved

That will do the job ;)

{% do assets.addJs('js/filename.js') %}