【问题标题】:jiren/filter.js with express and ejs files - conflicts带有 express 和 ejs 文件的 jiren/filter.js - 冲突
【发布时间】:2018-08-15 22:14:03
【问题描述】:

我正在创建一个包含 express/sequelize 和 ejs 文件的应用程序。 现在我想使用 Jiren 提供的这个出色的过滤器解决方案,但这个解决方案使用与 ejs 变量相同的语法。 当我在脚本标签type="text/html" 中包含带有这些变量的模板时,它会触发我一个错误(未定义但出现在我的包含行中)。当我将模板更改为“john do”时,页面正在显示,但我的控制台给我带来了“未定义错误 JSONquery”(我不确定可能是两个不同的错误,但 JSONquery 是 filter.js 插件的一部分) .我可以在这里给你看我的模板

<script id="artists-template" type="text/html">
    <li 
         class="main__product-list-item" 
         data-height="<%= height %>" 
         data-width="<%= width %>" 
         data-low="<%= yearBegin %>"
         data-high="<%= yearEnd %>" 
         data-name='<%= artist %>' 
         data-longueur='<%= height %>' 
         data-largeur='<%= width%>'
         >
            <figure class="main__product">
             <a class="main__product-image-link" href="/name/<%= id %>">
            <% if(Images.length !== 0) { %>
            <img alt="illustration" src="./img/_thumb<%= Images[0]['adress'] %>.jpg"/>
            <% } else { %>
            <img alt="illustration" src="./img/icon-no-image.svg"/>
            <% } %>
        </a>
        <figcaption class="main__product-description">
            <h3>
               <a href="/name/<%= id %>">
                  <span class="product__title">"<%= title %>" - <%= artist %></span>
                   <span class="product__date">, <span class="product__period"><%= period %></span></span>
                </a>
            </h3>
            <p>
            Créé probablement entre <span class="product__low-date"><%= yearBegin %></span> et <span class="product__high-date"><%= yearEnd %></span>
            <br/>Hauteur : <span class="product__height"><%= height %></span>  cm / Largeur : <span class="product__width"><%= width %></span> cm

            <span class="main__product-description-story"><%= description %></span></p>
        </figcaption>
    </figure>
     </li>
 </script>

当我只注入像&lt;%= artist %&gt; 这样的一个变量时,同样的问题。这是我绑定数据的方式:

<% var datas = JSON.stringify(creations) %>
<script>
    var data = <%- datas %>;
    console.log(data);
 </script>

我希望这只是一个简单的问题,我只需要更改一个简单的字符即可使其正常工作。这个时候我认为最大的问题是关于这个“JsonQUERY 未定义”,因为即使我没有将我的模板与数据绑定,必须用模板填充的区域实际上没有绑定。 我试图用谷歌搜索错误,但我什么也没找到。如果有人有任何想法...谢谢。

这里是过滤器解决方案的网站:https://github.com/jiren/filter.js

【问题讨论】:

    标签: javascript express filter ejs


    【解决方案1】:

    Hourrayyy !!!我找到了解决方案,我用谷歌搜索了 ejs 冲突,发现关键是要更改插件语法:如果遇到同样的问题,请尝试更改这些行:

    var templateSettings = {
        evaluate    : /<%([\s\S]+?)%>/g,
        interpolate : /<%=([\s\S]+?)%>/g,
        escape      : /<%-([\s\S]+?)%>/g
     };
    

    对这些

    var templateSettings = {
       interpolate: /\{\{(.+?)\}\}/g,
       escape: /\{\{\-(.+?)\}\}/g,
       evaluate: /\{\%(.+?)\%\}/g
    };
    

    现在我只需要使用这个语法:{{ variable }} 并且它可以正常工作!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-24
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      相关资源
      最近更新 更多