Skip to content

Latest commit

 

History

History
24 lines (17 loc) · 759 Bytes

generate_url_javascript.rst

File metadata and controls

24 lines (17 loc) · 759 Bytes

How to Generate Routing URLs in JavaScript

If you're in a Twig template, you can use the same path() function to set JavaScript variables. The escape() function helps escape any non-JavaScript-safe values:

<script>
var route = "{{ path('blog_show', {'slug': 'my-blog-post'})|escape('js') }}";
</script>

But if you actually need to generate routes in pure JavaScript, consider using the FOSJsRoutingBundle. It makes the following possible:

<script>
var url = Routing.generate('blog_show', {
    'slug': 'my-blog-post'
});
</script>