javascripthtmlmeteormeteor-helper

Helper causing text to not show up


The helper is supposed to look for hashtags and make them a route/link. Instead, it makes the text not show up. How do I fix this?

Here is the code:

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>

Solution

  • Try this:

    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>");
    
    });
    

    Note:

    Remember to put helper in tripple curly braces as it returns html;

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

    Proof