一次只能使用一个Handlebar Helper是stripTags还是截断,不是两者都是?

问题描述

我正在尝试在{{body}}上使用两个Handlebar Helper,但是其中只有一个正在工作吗? 我在这里https://youtu.be/SBvmnHTQIPY于1:44:10遵循Traversy media的Node.js App从头开始在youtube课程上学习。我使用diffchecker对他的代码进行了三重检查。这是我在模板index.hbs上使用的两个帮助器:

 var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                
 foreach (FilterInfo videoDevice in videoDevices)
 {
    VideoCaptureDevice camera new AForge.Video.DirectShow.VideoCaptureDevice(videoDevice.MonikerString);
    Debug.Print(camera.IsRunning)
 }

但是,一旦我添加“ 150”来截断,助手“ stripTags”将停止工作并显示html,但确实将其截断为150个字符。从这里:

J故事2我是婴儿marfa图画书教堂钥匙,vape绑腿8位主题标签umami tbh mixtape chillwave。屠夫陈词滥调的独角兽,您的VHS自行车使用权的烈性醋。唤醒了汽具艺术派对地铁瓷砖。现成的小批量腹腔康普茶,正大坡道连帽衫工匠班卓琴羽衣甘蓝芯片。奥斯汀符号学徒步旅行者pinterest,四美元吐司gochujang humblebrag绑腿。内脏8位可持续滑板藜麦。

对此:

J Story 2

车把助手代码

<p>{{stripTags (truncate body 150)}}</p>

App.js

const moment = require('moment')

module.exports = {
    formatDate: function (date,format) {
        return moment(date).utc().format(format)
    },truncate: function (str,len) {
        if (str.length > len && str.length > 0) {
            let new_str = str + ' '
            new_str = str.substr(0,len)
            new_str = str.substr(0,new_str.lastIndexOf(' '))
            new_str = new_str.length > 0 ? new_str : str.substr(0,len)
            return new_str + '...'
        }
        return str
    },stripTags: function (input) {
        return input.replace(/<(?:.|\n)*?>/gm,'')
    },}

解决方法

因此,我修复了添加故事模板中的拼写错误,但拼错了'id',但是当我修复它时,问题仍然没有消失!我移动了一些逗号,并修复了一些似乎不会影响页面的其他错别字。但是,当我将“ stripTags”移到括号内并“截断”括号外时,它突然起作用了!为什么现在可以正常工作?嗯。

“故事索引”页面中的固定代码:

<h1>Stories</h1>
<div class="row">
    {{#each stories}}
    <div class="col s12 m4">
        <div class="card">
            <div class="card-image">
                {{!-- todo: editIcon --}}
            </div>
            <div class="card-content center-align">
                <h5>{{title}}</h5>
                <p>{{truncate (stripTags body) 200}}</p>
                <br>
                <div class="chip">
                    <img src="{{user.image}}" alt="">
                    <a href="/stories/user/{{user._id}}">{{user.displayName}}</a>
                </div>
            </div>
            <div class="card-action center-align">
                <a href="/stories/{{_id}}" class="btn grey">Read More</a>
            </div>
        </div>
    </div>
    {{else}}
        <p>No Stories To Display</p>
    {{/each}}
</div>
,

目前正在阅读确切的教程,我遇到了这个问题。

在玩弄代码后,我注意到通过更改调用函数或助手的顺序可以解决问题。

那不是

<p>{{stripTags (truncate body 150)}}</p>

我用过

<p>{{truncate (stripTags body) 100}}</p>

为什么会这样??

我本身不知道,我不是正则表达式专家,只是学习 JS。然而,我确实注意到布拉德使用的正则表达式希望看到标签 但是当首先调用 truncate 时。在某些情况下,它可能会截断“>”。所以在调用 truncate 之前先调用 stripTags 辅助函数是我的选择