search
Homephp教程php手册twig里使用js变量的方法,twig使用js变量

twig里使用js变量的方法,twig使用js变量

本文实例讲述了twig里使用js变量的方法的方法。分享给大家供大家参考,具体如下:

先看一段代码

<script type="text/javascript">
  jQuery(document).ready(function(){
    jQuery(#my_input).change(function(){
      var value = jQuery(#my_input).val();
      jQuery.ajax({
        url: {{ path('ParteAccidentes_ajax', {'emergencia': value}) }},
        timeout: 5000,
        success: function(data) {
          alert('ok');
        },
        error: function() {
          alert('mal');
        }
      });
    });
  });
</script>

这个ajax请求的地址,无法正常正常访问。

这段代码里 jQuery("#my_input").val()的值赋值给value,然后想把value这个变量引入到ajax里的url地址里。

这时候你会发现,你访问的这个地址,value的值并未引入进来,而是把当做字符串进行处理。

也就是说js的值,是不能直接引用到twig里的。

原因是,twig解析的是php变量,而value是js变量,所以twig 默认认为是个字符串。

所以我们需要进行替换,就要用到replace

代码如下,大家可以和上面的代码自行对比:

<script type="text/javascript">
  jQuery(document).ready(function(){
    jQuery(#my_input).change(function(){
      var value = jQuery(#my_input).val();
      var url = "{{ path('ParteAccidentes_ajax', {'emergencia': 'text'}) }}";
      url = url.replace("text", value);
      jQuery.ajax({
        url: url,
        timeout: 5000,
        success: function(data) {
          alert('ok');
        },
        error: function() {
          alert('mal');
        }
      });
    });
  });
</script>

本文永久地址:http://blog.it985.com/7020.html
本文出自 IT985博客 ,转载时请注明出处及相应链接。

更多关于PHP模板相关内容感兴趣的读者可查看本站专题:《PHP模板技术总结》、《smarty模板入门基础教程》、《codeigniter入门教程》及《ThinkPHP入门教程》

希望本文所述对大家PHP程序设计有所帮助。

您可能感兴趣的文章:

  • twig模板常用语句实例小结
  • Twig模板引擎用法入门教程
  • 在Yii框架中使用PHP模板引擎Twig的例子
  • twig模板获取全局变量的方法
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.