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

Integrating volt into javascript file

Hello - I've got a javascript function that includes some volt language that I'd like to break out into its own javascript file:

function savePost(id, btn){
    $.get('{{ url('logs/cart/add/') }}' + id, function(data){
        // to stuff here
    })
}

If i include this on the .volt page, it works fine -- because volt parses the page and all is good. However, I can't put this in a .js file, since volt doesn't parse those.

Is the best way to do this to just create the function in the template that those particular pages extend?

I would use some inline javascript to simply declare a variable in your Volt template - "url_add_cart" for example - and set the value there. You can then reference that variable from your savePost() function, in the separate javascript file.