【问题标题】:How to access to a javascript function inside another html.twig file in symfony2?如何访问 symfony2 中另一个 html.twig 文件中的 javascript 函数?
【发布时间】:2014-09-05 10:04:03
【问题描述】:

我想知道如何访问 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 html function symfony execution


    【解决方案1】:

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

    执行者.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>
    

    文件2.html.twig

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

    【讨论】:

    • 好吧,正如我已经说过的,我实际上需要的是仅在文件“file2.html.twig”中执行函数 executer()。在这里,触发器在文件 file2.html.twig 中提交表单。你知道怎么做吗?
    • @Nadim Akram 为什么不将两个 js 函数都添加到 twig 文件中,然后根据需要调用函数。这完全取决于您如何编写 js,executer() 返回什么? validate() 如何使用它?
    • 发布一些您想要实现的具体示例,我们可以更好地帮助您并为您提供所需的东西
    【解决方案2】:

    您也可以将函数放在一个 JS 文件中,然后在两个 twig 文件中加载。

     //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>
    

    请注意,如果您需要一些其他数据,也可以传递变量来验证和添加参数来执行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 2022-01-14
      • 2022-12-17
      • 2021-08-24
      • 1970-01-01
      相关资源
      最近更新 更多