简体   繁体   English

试图作为php执行的Javascript代码

[英]Javascript code trying to be executed as php

This is a wordpress plugin I've used Backbone Marionette to build the back-end. 这是我使用Backbone Marionette构建后端的wordpress插件。 The problem is that the template code (html and javascript) its being execute as php. 问题在于模板代码(html和javascript)被作为php执行。

Fatal error: Call to undefined function substring() in ../main-menu.php on line 304 致命错误:在第304行../main-menu.php中调用未定义的函数substring()

    <script type="text/template" id="amazon-result-item-view">
      <p class="small-text">
      <a href="<%= url %>" target="_blank" >
      <%= name.substring(0,30) %>...    //getting the php error at this line
      </a></p>

      <img width="100" src="<%= image_url %>" />
      <button data-product-index="<%= cid %>"
       class="tiny add-amazon-product">
      Add product</button>
    </script>

This is from a plugin that is working in several other WP sites my guess is that this one is parsing anything wrap by this <%= %> as php but not sure why... 这是从一个可在其他几个WP网站中工作的插件得出的,我猜这是正在将<%=%>换行的任何内容解析为php,但不确定为什么...

As @ivarni mentioned you are able to change what delimiters underscore uses to interpolate values. 正如@ivarni提到的那样,您可以更改下划线用于插入值的定界符。 For example you can pass the following regex to underscore to switch the delimiters to mustache style delimiters which should work in your case. 例如,您可以将以下正则表达式传递给下划线,以将分隔符切换为适合您的情况的胡须样式分隔符。

 _.templateSettings = {
       evaluate: /\{\[([\s\S]+?)\]\}/g,
       interpolate: /\{\{([\s\S]+?)\}\}/g, 
       escape: /\{\{-([\s\S]+?)\}\}/g
 };

You would then modify your template to something like this 然后,您可以将模板修改为如下形式

  <script type="text/template" id="amazon-result-item-view">
      <p class="small-text">
      <a href="{{ url }}" target="_blank" >
      {[ name.substring(0,30) ]}...
      </a></p>

      <img width="100" src="{{ image_url }}" />
      <button data-product-index="{{ cid }}"
       class="tiny add-amazon-product">
      Add product</button>
    </script>

You're using JSP syntax <%= %> instead of php syntax {[ whatever ]} . 您使用的是JSP语法<%= %>而不是php语法{[ whatever ]} In this case, your 4th line should be like this: 在这种情况下,您的第四行应如下所示:

{[ name.substring(0,30) ]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM