variable jquery inside twig template

前端 未结 2 706
被撕碎了的回忆
被撕碎了的回忆 2021-01-06 00:04

I\'m trying to use a jquery varaible inside a twig template to send by ajax, but I can\'t access to the jquery variable inside the twig:

My code is:



        
2条回答
  •  無奈伤痛
    2021-01-06 00:41

    Of course it does not exist.

    The value variable is just plain text for Twig. Remember:

    First the Twig parts are rendered, then it gets outputted to the browser, then the Browser renders the content and executes the Javascript.

    The line {{ path('ParteAccidentes_ajax', {'emergencia': value}) }} is executed way before value is event parsed as javascript.

    Also you're doing this in an event handler.

    How should twig (since it's just a PHP library) change the path url on a executed javascript event?

    Since you just want to update an URL based on symfony paths and a javascript variable, please have a look at this bundle: https://github.com/FriendsOfSymfony/FOSJsRoutingBundle

    It provides the exact functionality you want.

提交回复
热议问题