javascript - How to compare twig value with JS value? -


situation : need compare js variable value twig variable value, inside jquery function or somehow access specific object twig array based on given id number.

the code describes situation :

            var id = ...;             {% user in userlist %}              {% if user.id == id %}                  var userinfo = $.parsejson({{ user }};              {% endif %}             {% endfor %}              $('#username-field').html(userinfo['username']);             ... 

or similar (if possible) :

            var id = ...;             var userinfo = $.parsejson({{ userlist|findbyfield('id', id }};              $('#username-field').html(userinfo['username']);             $('#title-field').html(userinfo['title']); 

problem : since twig not able access js value, i have no idea, how place kind of placeholder inside twig {% ... %} brackets.

it nice access selected object collection send request twig template, instead of creating new ajax request, particular object once again controller.

twig server-side template engine. unfortunately, can't compare twig variables , javascript variables. therefore should pass crucial variable id server side. so, userlist , id must twig variables.

{% user in userlist %}  {% if user.id == id %}      var userinfo = {{ user.username|json_encode|raw }}; // or {{ user.username|raw }}  {% endif %} {% endfor %}  $('#username-field').html(userinfo); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -