最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Handlebars function to format currency with javascript - Stack Overflow

programmeradmin3浏览0评论

I have this in my handlebar template:

<span class="currencyFormatMe">{{_current_price}}</span>

An example of what the loop returns|: Bid: $24000

I'd like to format that with commas and i'm failing.

I have this function which works in the console, but fails when adapted to the codebase with handlebars.

$.fn.digits = function(){ 
    return this.each(function(){ 
        $(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") ); 
    })
}

And I call it like $("span.currencyFormatMe").digits();

Again it all works in the console, but fails when adapted. Any pointers are very welcome

Tried it with a registerhelper:

Handlebars.registerHelper('formatCurrency',
    $.fn.digits = function(){ 
        return this.each(function(){ 
            $(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") ); 
        })
    }
);

Calling:

{{formatCurrency _current_price}}

I have this in my handlebar template:

<span class="currencyFormatMe">{{_current_price}}</span>

An example of what the loop returns|: Bid: $24000

I'd like to format that with commas and i'm failing.

I have this function which works in the console, but fails when adapted to the codebase with handlebars.

$.fn.digits = function(){ 
    return this.each(function(){ 
        $(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") ); 
    })
}

And I call it like $("span.currencyFormatMe").digits();

Again it all works in the console, but fails when adapted. Any pointers are very welcome

Tried it with a registerhelper:

Handlebars.registerHelper('formatCurrency',
    $.fn.digits = function(){ 
        return this.each(function(){ 
            $(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") ); 
        })
    }
);

Calling:

{{formatCurrency _current_price}}
Share Improve this question edited Jan 24, 2013 at 1:15 jahrichie asked Jan 24, 2013 at 0:51 jahrichiejahrichie 1,2353 gold badges17 silver badges27 bronze badges 2
  • shouldn't you be using a registerHelper – epascarello Commented Jan 24, 2013 at 0:58
  • @epascarello updated the above code to reflect my attempt at a helper....still no luck. Anything standing out? – jahrichie Commented Jan 24, 2013 at 1:16
Add a comment  | 

1 Answer 1

Reset to default 23

You have a couple simple options here.

You can stick with your jQuery plugin and apply it after the Handlebars template has been filled in; something like this:

<script id="t" type="text/x-handlebars">
    <span class="currencyFormatMe">{{_current_price}}</span>
</script>

and then:

$.fn.digits = function(){ 
    return this.each(function(){ 
        $(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") ); 
    })
};

var t = Handlebars.compile($('#t').html());
var h = t({
    _current_price: 1000
});
$('<div>').append(h).find('.currencyFormatMe').digits();

Demo: http://jsfiddle.net/ambiguous/732uN/

Or you can convert your plugin into a Handlebars helper and do the formatting in the template. If you want to do this you just have to format the value passed to the helper rather than messing around with $(this) inside the helper. For example:

<script id="t" type="text/x-handlebars">
    {{formatCurrency _current_price}}
</script>

and then:

Handlebars.registerHelper('formatCurrency', function(value) {
    return value.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
});

var t = Handlebars.compile($('#t').html());
var h = t({
    _current_price: 1000
});

Demo: http://jsfiddle.net/ambiguous/5b6vh/

发布评论

评论列表(0)

  1. 暂无评论