如何访问symfony2中另一个html.twig文件中的javascript函数? [英] How to access to a javascript function inside another html.twig file in symfony2?

查看:134
本文介绍了如何访问symfony2中另一个html.twig文件中的javascript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何访问symfony2中另一个html.twig文件中的JavaScript函数。假设我们有两个html.twig文件:file1.html.twig和file2.html.twig。我们还假设他们的代码如下所示:

file1.html.twig的代码:

 < html> 
< head>
< script>
函数validate(){
//我需要放在这里运行javascript函数执行器()的代码file2.html.twig中存在
}
< /脚本>
< / head>
< body>
< form id =EventFormaction ='{{path('ikproj_groupe_homepaeventsAdd',{id:idg})}}'method =POST{{form_enctype(form)}} onsubmit =validate );>
//这里是表单内容。
< / form>
< / body>
< / html>

file2.html.twig的代码:

 < html> 
< head>
< script>
函数executableter(){
//函数内容
}
< / script>
< / head>
< body>

< / body>
< / html>

实际上,我想要做的是提交file1.html.twig中的表单将在文件file2.html.twig内执行函数执行器()。是否有可能在Symfony2中做到这一点??如果是的话,我应该在文件file1.html.twig的函数validate()中放入什么?

解决方案

您可以将javaScript放入它自己的twig文件中,并将其包含在您的其他twig文件中(如需要)。
示例(为了完整性传递参数);

executer.html.twig

 < script> 
函数validate(){
// javascript函数执行器()
var foo = {{foo}};
}
< / script>

file1.html.twig

 < HTML> 
< head>
{%includeexecuter.html.twig与{'foo':10}%}
< / head>
< body>
< form id =EventFormaction ='{{path('ikproj_groupe_homepaeventsAdd',{id:idg})}}'method =POST{{form_enctype(form)}} onsubmit =validate );>
//这里是表单内容。
< / form>
< / body>
< / html>

File2.html.twig

 < HTML> 
< head>
{%includeexecuter.html.twigwith {'foo':20}%}
< / head>
< body>

< / body>
< / html>


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 ?

解决方案

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>

这篇关于如何访问symfony2中另一个html.twig文件中的javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆