0

I am currently working on a symfony2.0 project. At the moment I am stuck at some point where I want to use some simple javascript within my twig file.

From my controller I pass an array of entities called Machine to the twig file like this:

 ...
return $this->render('PRwissHostsBundle:mini:editLocation.html.twig',
       array(
         'form' => $form->createView(), 'id' => $id, 'machines' => $machinesInLoc, 
       ));

My form inside the twig file easily cann acces the machines array. What I now need is to access this array within javascript.

Currently I m doing this like follwing:

<script type="text/javascript">
    ...
    var mach_array = {{machines|json_encode|raw}};
    var machine = mach_array[0];
    alert(machine.name);
    ....
</script>

Somehow if I alert the mach_array it says that it is an object. Same result when I alert the machine. What is not possible is to access the machines id or name or whatevers property of it.

I have searched a couple of other questions like this but unfortunatly they were not helpfull regarding an array of entities.

Any help is highly appreciated.

4
  • Did you fully print the mach_array so see what's in ? Commented Feb 6, 2015 at 10:53
  • I suggest you to use console.log(mach_array) to check your data and with Twigg you can use {{ dump(machines) }} to see what are inside your response. Commented Feb 6, 2015 at 10:53
  • if I print mach_array it says {} Commented Feb 6, 2015 at 11:48
  • 1
    Then I think it's more likely to be a query problem. I think $machinesInLoc is an empty array. Commented Feb 6, 2015 at 15:30

2 Answers 2

1

In general, you shouldn't let Twig handle the data formatting unless it's absolutely necessary, for example as a response from a AJAX call.

With that said, your issue lies with how you declare the mach_array.

var mach_array = {{machines|json_encode|raw}};

should be

var mach_array = '{{machines|json_encode|raw}}';

By not wrapping the call to twig, Javascript will make mach_array a Object, its the same as

var mach_array = {"foo" : "bar"}

which resolves to a Object.

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

2 Comments

if I log the mach-array it just says {}.. looks like an empty array
Then try logging '{{machines|json_encode|raw}}' and see if you get the expected JSON string in your console.
1

So I just solved my problem with the help of Nihilnovi. The problemw as not an empty array like gregory supposed, I just did not realy figure out how to properly use javascript and twig entities. The working code now looks like following:

<script type="text/javascript">
function report(period){
  var e = document.getElementById("form_machines");
  var selectValue = e.options[e.selectedIndex].value;
  var selectText  = e.options[e.selectedIndex].text;
  if (selectValue != ""){
    var table = document.getElementById("uebersicht");
    var row = table.insertRow(-1);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    var cell3 = row.insertCell(2);
    cell1.innerHTML = "<b>Name:</b>";
    var mach_array = {{machinesAvailable|json_encode|raw}};        

    {% for machine in machinesAvailable %}
      if ({{machine.name|json_encode|raw}}  == selectText){
       cell2.innerHTML = "<a href='{{ url('PRwissHostsBundle_det_machine', { 'id':machine.id }) }}'> {{machine.name}} </a>";
      }
    {% endfor %}

    cell3.setAttribute('align', 'right');
    cell3.innerHTML = "<input type='checkbox' id=machine.id >";
  }
}


</script>

Hope this helps some who steps into the same issue!

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.