duanchu7271 2014-04-03 06:49
浏览 100
已采纳

如何将javascript集成到phalcon php的.volt模板引擎中

I need to use http://blueimp.github.io/jQuery-File-Upload/ in my project which use framework PhalconPHP

In order to do so, my .volt file need to contain a javascript code like this

<script id="template-upload" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %} //The problem begin here
 <tr class="template-upload fade">
  <td>
  <span class="preview"></span>
    ........//Some similar code here
  </td>
 </tr>
{% } %}
</script>

But the problem is {% and %} is .volt template syntax. When I use {% for (var i=0, file; file=o.files[i]; i++) { %} like that, the .volt syntax and javascript syntax are conflict. Browser such as Chrome or Firefox will show the error : "Syntax error, unexpected token ( in /var/www/.... on line 77" where 77 is that line start with {%

In .phtml it works fine, but I don't want to rebuild my whole view template with .phtml How can I use this code with .volt? Is there other syntax for javascript which is different from {% and %} ? Thank you!

  • 写回答

4条回答 默认 最新

  • dqiaw48488 2014-04-03 14:03
    关注

    @jodator has a good approach.

    Alternatively you can use PHP in your Volt template

    <script id="template-upload" type="text/x-tmpl">
    <?php foreach (.....) { ?>
       <tr class="template-upload fade">
       <td>
           <span class="preview"></span>
           ........//Some similar code here
       </td>
       </tr>
     <?php } ?>
     </script>
    

    The only issue here is that you have to be careful on what the scope of your variables are so that PHP can process them. For instance if o.files is a javascript object then you need to pass it as a variable in PHP. If it is a PHP object then all you will have to do is change it to $o.files

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?