php爆炸不能在javascript模板中工作
I'm aware of some posts already dealing with the same topic.
Following are the links:
Javascript Template Engine Use with jQuery
I'm trying to incorporate a php explode function into a javascript template. The plugin is jQuery-File-Upload from blueimp @ https://github.com/blueimp/jQuery-File-Upload/downloads i downloaded off GitHub.
Following is part of my code i'm trying to execute and make it work.
<!-- The template to display files available for download -->
<script id="template-download" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr>
<td>
<?php echo "{%=file.name%}"; ?> //{%file.name%} contains a string seperated by underscores
<?php $file_name_long = '{%=file.name%}'; ?>
<?php $file_name = explode ('_' , $file_name_long); ?>
<?php print_r($file_name); ?>
</td>
</tr>
...
...
I'm able to make $file_name print out as an array but I'm not able to get it seperated by the underscore within my file.name. the printed array contains the whole string as per file.name.
I'm not sure if this is even possible. I've been trying all day. If this is not do-able or logically incorrect, do let me know so that i can stop trying to want to make this work. Thanks and appreciated.
我知道有些帖子已经涉及同一主题。 p>
以下是链接: p>
什么是x-tmpl? p>
我正在尝试将php爆炸功能合并到一个javascript模板中。 该插件是来自blueimp的 https://github.com/blueimp / jQuery-File-Upload / downloads 我从GitHub上下载了。 p>
以下是我的代码的一部分,我正在尝试执行并使其正常工作。 p> \ n
&lt;! - 显示可供下载的文件的模板 - &gt;
&lt; script id =“template-download”type =“text / x-tmpl”&gt; \ n {%for(var i = 0,file; file = o.files [i]; i ++){%}
&lt; tr&gt;
&lt; td&gt;
&lt;?php echo“{%= file 。名称%}”; ?&GT; // {%file.name%}包含一个由下划线分隔的字符串
&lt;?php $ file_name_long ='{%= file.name%}'; ?&gt;
&lt;?php $ file_name = explode('_',$ file_name_long); ?&gt;
&lt;?php print_r($ file_name); ?&gt;
&lt; / td&gt;
&lt; / tr&gt;
...
...
code> pre>
我能够制作 $ file_name作为数组打印出来但我无法通过 file.name strong>中的下划线将其分开。 打印的数组按照 file.name strong>包含整个字符串。 p>
我不确定这是否可行。 我一整天都在努力。 如果这不可行或逻辑上不正确,请告诉我,以便我可以停止尝试使这项工作。 谢谢和赞赏。 p>
div>
your are trying to read javascript values in your php code.
{%=file.name%}
is simply a string to php and contains no actual filenames when your php renders the output. so your print_r() would output {%=file.name%}
which then gets translated into your filename later in the browser after the whole php processing is done.
try implementing this stuff in javascript alone.
You're trying to execute server-side code (php) on the client (in your js template). The code {%=file.name%}
hasn't been replaced at the time that your php code is trying to execute it (because that's on the server).
If you mix server-side and client-side code, you really need to manage your logic to account for the server executing before the client.