[英]How to access to a javascript variable inside another html.twig file in symfony2?
[英]How to access to a javascript function inside another html.twig file in symfony2?
我想知道如何訪問symfony2中另一個html.twig文件中的javascript函數。 假設我們有兩個html.twig文件:file1.html.twig和file2.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>
file2.html.twig的代碼:
<html>
<head>
<script>
function executer(){
//the function content
}
</script>
</head>
<body>
</body>
</html>
實際上,我想做的是提交file1.html.twig中的一種形式,在file2.html.twig文件中將執行函數executer()。 可以在Symfony2中做到這一點嗎?...如果是,我應該在文件file1.html.twig的函數validate()中放入什么?
您可以將javaScript放在它自己的樹枝文件中,並根據需要將其包含在其他樹枝文件中。
示例(僅出於完整性目的傳遞參數);
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>
您還可以將函數放入兩個樹枝文件中加載的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>
請注意,如果您需要其他數據,則可以傳遞變量來驗證並添加參數以執行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.