Twig & backbone.js Uncaught SyntaxError: Unexpected token &

Trying to preload an backbone collection with a native Symfony2 array variable in Twig and getting smacked out with the error: Uncaught SyntaxError: Unexpected token &

Use the following lines inside your twig template to do this:

<script>
var accounts = new Backbone.Collection;
  {% autoescape false %}

         accounts.reset({{ eventtable|json_encode() }});

  {% endautoescape %}
</script>

The whole trick is the “autoescape false”, after adding this around your backbone assignment it should work just fine. 

(sorry just moved over from Posterous which was generously killed by twitter, haven’t had any time to proper style this WP install)

Leave a comment