【问题标题】:Helper causing text to not show up帮助导致文本不显示
【发布时间】:2014-10-30 22:27:00
【问题描述】:

助手应该查找主题标签并将它们设为路由/链接。相反,它使文本不显示。我该如何解决这个问题?

代码如下:

handlebar.js

Handlebars.registerHelper('hashtag', function(posttext) {
    posttext.html();
    posttext = posttext.replace(/#(\w+)/g, "<a href='https://www.google.com/?q=$1' target='_blank'>$&</a>");
    posttext.html(posttext);
});

postidem.js

<h3 class="text">{{hashtag title}}</h3>
<p class="text">{{hashtag posttext}}</p>

【问题讨论】:

    标签: javascript html meteor meteor-helper


    【解决方案1】:

    试试这个:

    Handlebars.registerHelper('hashtag', function(posttext) {
        // DEBUG START : just to know that `posttext` is really what you want; remove later;
        console.log("hashtag",posttext);
        // DEBUG END
    
        return posttext.replace(/#(\w+)/g, "<a href='https://www.google.com/?q=$1' target='_blank'>$&</a>");
    
    });
    

    注意:

    • Template.registerHelper 适用于 Meteor 0.9.1 及更高版本
    • Handlebars.registerHelper 适用于每个版本。
    • 不要在 Meteor.startup 中定义全局助手,因为 Blaze 将无法找到这些助手。

    记得在返回 html 时将 helper 放在三重花括号中;

    <h3 class="text">{{{hashtag title}}}</h3>
    <p class="text">{{{hashtag posttext}}}</p>
    

    Proof

    【讨论】:

    • 也许使用最新的Template.registerHelper ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 2010-09-05
    • 1970-01-01
    相关资源
    最近更新 更多