Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

retrieve parameters.yml in javascript

Can i retrieve the global parameters in file javascript?

i have for example

twitter.app_secret: my_token

how can i retrieve it in file Javascript ?

for now i use this

<script type="text/javascript">
    var app_assoluto = '{{ app.request.getSchemeAndHttpHost() }}';
    var consumer_key_twitter = '{{ twitter_app_id }}';
    var consumer_secret_twitter = '{{ twitter_app_secret }}';
    var access_token_twitter = '{{ twitter_access_token }}';
    var access_token_secret_twitter = '{{ twitter_token_secret }}';
    var facebook_app_id = '{{ facebook_app_id }}';
    var env = '{{ app.environment }}';
</script>

also parameters like {{ app.request.getSchemeAndHttpHost() }}

there is a better solution for retrieve this param??

like image 549
Barno Avatar asked Oct 02 '22 13:10

Barno


1 Answers

I noticed that you are use twig templates. So, you can make your parameters as twig globals like that:

# app/config/config.yml
twig:
    # ...
    globals:
        key: "%your_value%"

And in your templates you have access to key via {{ key }}

UPDATE

If you are using jQuery, you can do that in this way:

# template.html.twig
<script>
    var params = params || {};
    $.extend(params, {
        twitterAppId: '{{ twitter_app_id }}'
    });
</script>

and in your JS file:

# script.js
(function() {
    params = params || {};
    twitterAppId = params.twitterAppId ||{};
)();

I hope that this helps.

like image 112
NHG Avatar answered Oct 13 '22 11:10

NHG