1

In symfony2, this works fine in the html.twig file:

<a class="btn brl-next brl-btn" href="{{ path('new_message') }}">Next</a>

but currently, i need to access path('new_message') by javascript.

I tried:

        nextButtons.each(function () {
            $(this).on('click', function () {
                $("a").prop("href", "{{ path('new_message') }}");
            });
        });

But it's not working, does someone can help me on how to access symfony path by javascript?

4
  • 1
    You can use FOSJsRoutingBundle (develop by SF) Commented Jul 21, 2014 at 13:34
  • ^ +1 use that bundle, so you don't have to reinvent the wheel. Commented Jul 21, 2014 at 13:44
  • You can add the routes to your anchor tags in your twig template like explained here: stackoverflow.com/a/24674562/1791606 . Meaning you can have standalone javascript files rather mixing javascript and html in your template files. Commented Jul 21, 2014 at 13:51
  • @Debflav I like the ideao of FOSJsRoutingBundle, but it depends on how often you need that. For just a simple ajax task where you want to define your route in routing.yml you also can make a simple div with an attribute, as i described below :) Commented Jul 21, 2014 at 19:15

4 Answers 4

1

Just add an HTML Element with an attribute with the path. I use that e.g for getting my ajax route. See example below:

dyncontent.html.twig

<div id="dyncontent" class="dyn" data-ajaxurl="{{ path("common_ajax") }}"></div>

i need that div for loading data into it, and as path information i have the data-ajax attribute with my path defined.

now i can select this path with my Javascript

dyncontent.js

var ajaxpath =  $("#dyncontent").attr("data-ajaxurl");
$.getJSON(ajaxpath , null, function(data) {
  //get JSON out of the right controller with correct path
}

I am not sure what you are trying with your buttons, but you can also make

button.html.twig

<a class="btn brl-next brl-btn" href="#" data-path="{{ path('new_message') }}">Next</a>

and get the information with

button.js

var new_message_path = $(".brl-btn").attr("data-path");

now you have your path in the new_message_path variable. But think about making your selector unique.

Sign up to request clarification or add additional context in comments.

Comments

0

You can do this like that:

In your layout.html.twig define global js vars like this:

<script type="text/javascript">
    var routes = {
        myroutes:{
            route1:"{{ path('my_symfony_route', {}, true) }}", // the true permits to use relative path
            route1:"{{ path('my_symfony_route2', {}, true) }}" 
        },
    };
</script>

Then in yourjsfile.js call global depending on your needs

nextButtons.each(function () {
    $(this).on('click', function () {
        $("a").prop("href", routes.myroutes.route1);
    });
});

I hope this will help you!

9 Comments

it does not work: it will direct go to : /{{ path('new_message', {}, true) }}
This is strange, it works for me... could you test your variables visibility via your firebug console?
are you using FOSJsRoutingBundle ?
No i don't use FOSJsRouting.
you can try without options i put in the example: routes = { myroutes:{ route1:"{{ path('my_symfony_route') }},
|
0

For my case, I use all the path

For example if your route's name is: "new_message", and the path is /yourproject/web/app_dev.php/message/new,you can make like that in javascript:

var route='/docMessagerie/web/app_dev.php/protocol/updateProtocol/';

It's working for me

Comments

0

You can directly add twig path syntax like this.

var commercial = '{{path('_commercial')}}';

then you can call like this

 window.location = commercial ;

Comments

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.