symfony - Call JavaScript function from Twig -


i'm working on symfony2 project , want call javascript function , pass array of json objects (which controller) twig.

but first, simple test failed, like:

main.js:

function helloworld(name) {    console.log("hello " + name); } 

linked main.js in twig , called function:

<body>     <script>helloworld("world!")</script>      {% block javascripts %}         <script src="{{ asset('js/main.js') }}"></script>     {% endblock %} </body> 

which results in referenceerror:

"uncaught referenceerror: helloworld not defined"

what have differently make work?

edit: 2 took time answer. described twig consists of bunch of nested twigs , placement of javascript include based on symfony documentation, guess that's why didn't see obvious. should have detected problem myself when phrasing question though....

invert order of functions:

<body> {% block javascripts %}     <script src="{{ asset('js/main.js') }}"></script> {% endblock %}  <script>helloworld("world!")</script> </body> 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -