[英]How to access to a javascript function inside another html.twig file in symfony2?
I would like to know how to access to a javascript function inside another html.twig file in symfony2. 我想知道如何访问symfony2中另一个html.twig文件中的javascript函数。 Let's assume that we have two html.twig files: file1.html.twig and file2.html.twig .
假设我们有两个html.twig文件:file1.html.twig和file2.html.twig。 Let's also assume that their codes are as below:
我们还假设它们的代码如下:
the code of file1.html.twig: 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: 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 . 实际上,我想做的是提交file1.html.twig中的一种形式,在file2.html.twig文件中将执行函数executer()。 Is it possible to do that in Symfony2 ??...If yes, what shall I put inside the function validate() of the file file1.html.twig ?
可以在Symfony2中做到这一点吗?...如果是,我应该在文件file1.html.twig的函数validate()中放入什么?
You could put the javaScript in it's own twig file and the include that in your other twig files as required. 您可以将javaScript放在它自己的树枝文件中,并根据需要将其包含在其他树枝文件中。
Example (passing a parameter in just for completeness); 示例(仅出于完整性目的传递参数);
executer.html.twig executer.html.twig
<script>
function validate(){
// javascript function executer()
var foo = {{ foo }};
}
</script>
file1.html.twig 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 File2.html.twig
<html>
<head>
{% include "executer.html.twig" with {'foo': 20} %}
</head>
<body>
</body>
</html>
You can also put the function in a JS file that you load in both twig files. 您还可以将函数放入两个树枝文件中加载的JS文件中。
//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. 请注意,如果您需要其他数据,则可以传递变量来验证并添加参数以执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.