0

I would like to know how to access to a javascript function inside another html.twig file in symfony2. Let's assume that we have two html.twig files: file1.html.twig and file2.html.twig . Let's also assume that their codes are as below:

the code of file1.html.twig:

 <html>
<head>
<script>
function validate(){
 //the code that I need to put here to run the javascript function executer() which exists in file2.html.twig
}
</script>
</head>
<body>
<form id="EventForm" action='{{path('ikproj_groupe_homepaeventsAdd',{id:idg})}}' method="POST" {{ form_enctype(form) }} onsubmit="validate();">
    //here is the form content.
</form>
</body>
</html>

the code of file2.html.twig:

 <html>
<head>
<script>
function executer(){
 //the function content
}
</script>
</head>
<body>

</body>
</html>

Actually, what I would like to do is that one the form in file1.html.twig is submitted there will be an execution of the function executer() inside the file file2.html.twig . Is it possible to do that in Symfony2 ??...If yes, what shall I put inside the function validate() of the file file1.html.twig ?

2 Answers 2

2

You could put the javaScript in it's own twig file and the include that in your other twig files as required.
Example (passing a parameter in just for completeness);

executer.html.twig

<script>
function validate(){
    // javascript function executer()
    var foo = {{ foo }};
}
</script>

file1.html.twig

<html>
    <head>
        {% include "executer.html.twig" with {'foo': 10} %}
    </head>
<body>
    <form id="EventForm" action='{{path('ikproj_groupe_homepaeventsAdd',{id:idg})}}' method="POST" {{ form_enctype(form) }} onsubmit="validate();">
    //here is the form content.
</form>
</body>
</html>

File2.html.twig

<html>
    <head>
        {% include "executer.html.twig" with {'foo': 20} %}
    </head>
<body>

</body>
</html>
Sign up to request clarification or add additional context in comments.

3 Comments

Well, as I already said, what I need actually is to execute the function executer() in the file "file2.html.twig" only. Here, the triger is submitting the form in the file file2.html.twig. Do you have any idea about how to do that?
@Nadim Akram why not add both js functions into the twig file and then just call the functions as required. It all depends on how you code the js, what does executer() return? how it is consumed by validate()?
post some concrete example of what you want to achieve we can help you better and give you what you need
0

You can also put the function in a JS file that you load in both twig files.

 //mycustomjsfile.js

 function executer(somevariable){
  //more codes..
 }

 function validate(somevariable){
  executer(somevariable);
  //more codes..
 }



 //twig file1
 <head>
 <script src="path/to/js/file"></script>
 <script>
  function validate({{somevariable}}){
    file2.html.twig
  }
 </script>
 </head>

 //twig file2
 <head>
 <script src="path/to/js/file"></script>
 <script>
  function execute({{somevariable}}){
    file2.html.twig
  }
 </script>
 </head>

Note that you can pass variable to validate and add parameters to execute also if you need some other data.

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.