0

I have app.js file and in it bunch of javascript code. Among other thing I want be ablt to add path.

var ul = " {{ path('chat_get_fnln', {'email':'%s'}) | url_decode | format('"+jid+"') | raw }}";

I need this in app.js not some twig file.

1) Is it such a thing even possible? 2) If yes please point me right direction.

I need this in .js file not in .twig.html!!!

3 Answers 3

2

You can also use global JS variable in twig template

<script>
    if (typeof app == 'undefined'){
    app = [];
}
app['your_url'] = '{{ path('your_route') }}';
app['other_value'] = 'value';
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

You could use hidden elements in your html, rendered by twig, and load them using js. Anyway as Amine posted use FosJsRoutingBundle, is the best way to generate paths for js.

2 Comments

this is how I did it. Thanks.
Other way could be to use Ajax to get any params you need. It would be more reusable and clean because you don't mix data and structure with this aproach
1

you can create a js file using a js.twig file. Or you can use FosJsRoutingBundle

after you can do:

Routing.generate('my_route_to_expose', { id: 10 });

1 Comment

This is not really a option. So no.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.