IAMTubby
IAMTubby

Reputation: 1677

Replacing   with white space in handlebars

From my REST API, I am passing in certain strings where white space is replaced by a white space.

When it comes to the handlebars component, I would like to replace these back with whitespace before it is displayed in the browser.

How can I replace a part of a string, white space, in this case, with   inside of the handlebars.

This is my code

<html>
<head>
    <table>
        <thead>
        </thead>
        <tbody>
        {{#goalSetValues}}
            <td><button onclick=init('{{goal}}')>{{goal}}</button></td>
        {{/goalSetValues}}
        </tbody>
    </table>

    <script type="text/javascript">

    function init (goalName) {
        console.log('came inside the init function with goalName == ' + goalName)

    }

    </script>
</head>
</html>

I have tried a couple of things,

<td><button onclick=init('{{goal}}')>{{replace goal " " "&nbsp;"}}</button></td>

and

<td><button onclick=init('{{goal}}')>{{#replace "&nbsp;" " "}}{{goal}}{{/replace}}</button></td>

However, they do not seem to work. Please advise me.

Upvotes: 2

Views: 4299

Answers (2)

Antony D&#39;Andrea
Antony D&#39;Andrea

Reputation: 1004

Use triple brackets: {{{}}} to output as raw HTML.

Then the &nbsp; will automatically be rendered as whitespace by the browser.

E.g.

<td><button onclick=init('{{{goal}}}')>{{{goal}}}</button></td>

Upvotes: 2

Behram Buhariwala
Behram Buhariwala

Reputation: 103

You could parse your goalSetValues array in such a way so as to replace the "&nbsp ;" in your string with an actual non breaking space.

var string = "Hello&nbsp;World"; var nbsp = String.fromCharCode(parseInt("00A0", 16)); string = string.replace("&nbsp;", nbsp);

This will make the string "Hello World" but the space you see is actually a non breaking space.

Upvotes: 0

Related Questions