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

How can I access Phalcon constant value in JS file?

Hi, I am try access my constants in javascript file.

I am try these methods;

var CDNUrl = "<?php echo CDNUrl; ?>";

var CDNUrl = "{{ constant(\"CDNUrl\") }}";

what is the best way for access any variable or constant in javascrpt file??

Thanks.



1.4k
Accepted
answer

Hi,

I usually do something like this:

$options = ['cdnUrl' => 'https://.../'];
$this->assets->collection('footer')->addInlineJs("App = " . json_encode($options) . ";");

In the view:

{{ assets.outputInlineJs('footer') }}

So you'll have in your javascript the variable App.cdnUrl.



5.1k

Thanks working :)

Hi,

I usually do something like this:

$options = ['cdnUrl' => 'https://.../'];
$this->assets->collection('footer')->addInlineJs("App = " . json_encode($options) . ";");

In the view:

{{ assets.outputInlineJs('footer') }}

So you'll have in your javascript the variable App.cdnUrl.