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.

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.



762
Accepted
answer

Hi,

I usually do something like this:

$options = ['cdnUrl' => 'http://.../'];
$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.



2.0k

Thanks working :)

Hi,

I usually do something like this:

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

In the view:

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

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