Javascript 代码试图作为 php 执行

Javascript code trying to be executed as php

这是我用 Backbone Marionette 构建后端的 wordpress 插件。问题是模板代码(html 和 javascript)被执行为 php.

致命错误:调用 ../main-menu.php 中的未定义函数 substring(),第 304 行

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

这来自一个在其他几个 WP 站点上工作的插件,我猜这个插件正在将这个 <%= %> 包装的任何内容解析为 php 但不确定为什么...

As @ivarni mentioned you are able to change what delimiters underscore 用于插入值。例如,您可以将以下正则表达式传递给下划线,以将分隔符切换为适用于您的情况的小胡子样式分隔符。

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

然后您可以将模板修改为类似这样的内容

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

您正在使用 JSP 语法 <%= %> 而不是 php 语法 {[ whatever ]}。在这种情况下,您的第 4 行应该是这样的:

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