【问题标题】:using default value with an ejs filter使用带有 ejs 过滤器的默认值
【发布时间】:2012-11-05 01:05:28
【问题描述】:

如果 item.description 未定义或为空,我想默认为“无描述”:

我尝试了以下方法:

  <%-: ( item.description | markdown ) || '<p>No description</p>' %>
  <%-: ( item.description || 'No description' ) | markdown %>

我还能做什么?

【问题讨论】:

    标签: node.js express ejs


    【解决方案1】:

    不确定您是否可以将|| 与 EJS 的filters| 混合使用,但您可以通过add a filter 来完成它:

    ejs.filters.or = function (arg, sub) {
      return arg || sub;
    };
    
    <%-: item.description | or:'No description' | markdown %>
    

    【讨论】:

    【解决方案2】:

    我想知道你的问题的代码,为什么带有 ejs 模板表达式的 pre-tag 是"&lt;%-" not the "&lt;%="(输出或赋值输出)或"&lt;%=:"(过滤器输出)签名?

    顺便说一句,过滤器调用签名是单竖线而不是双竖线,您可以随时根据需要调用...

    【讨论】:

    • markdown 生成 html,所以你需要
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 2017-05-02
    • 2021-05-22
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    相关资源
    最近更新 更多