I'm working on a Symfony2 project and I want to call a JavaScript function and pass an array of JSON objects (which I get from a controller) from a Twig.
But a first, very simple test already failed, like:
main.js:
function helloWorld(name) {
console.log(hello + name);
}
linked to main.js in the twig and called the function:
<body>
<script>helloWorld(world!)</script>
{% block javascripts %}
<script src={{ asset('js/main.js') }}></script>
{% endblock %}
</body>
which results in a ReferenceError:
Uncaught ReferenceError: helloWorld is not defined
What do I have to do differently to make this work?
EDIT: Thanks to the two who took the time to answer. The described twig actually consists of a bunch of nested twigs and the placement of the javascript include was based on the Symfony documentation, guess that's why I didn't see the obvious. Should have detected the problem myself when phrasing the question though....