如何将JavaScript集成到Phalcon php的.volt模板引擎中 [英] How to integrate javascript to .volt template engine of phalcon php

查看:80
本文介绍了如何将JavaScript集成到Phalcon php的.volt模板引擎中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 http://blueimp.github.io/jQuery-File-Upload /在我的项目中使用了框架PhalconPHP

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

为此,我的.volt文件需要包含这样的javascript代码

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>

但是问题是{%和%}是.volt模板语法.当我这样使用{% for (var i=0, file; file=o.files[i]; i++) { %}时,.volt语法和javascript语法会发生冲突.浏览器(例如Chrome或Firefox)将显示错误:语法错误,意外的令牌(在/var/www/....第77行中"),其中77是该行以{%

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 {%

在.phtml中它可以正常工作,但我不想使用.phtml重建我的整个视图模板 如何在.volt中使用此代码? javascript是否有其他语法与{%和%}不同? 谢谢!

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!

推荐答案

@jodator有一个很好的方法.

@jodator has a good approach.

或者,您可以在Volt模板中使用PHP

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>

这里唯一的问题是,您必须注意变量的范围,以便PHP可以处理它们.例如,如果o.files是javascript对象,则需要将其作为变量传递给PHP.如果它是一个PHP对象,则只需将其更改为$o.files

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

这篇关于如何将JavaScript集成到Phalcon php的.volt模板引擎中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆