MustachejQueryjavascript - how to perform method on mustache variable? - Stack Overflow

I have a simple mustache template setup that takes an object player and creates a list element. What�

I have a simple mustache template setup that takes an object player and creates a list element. What's the best way to perform a javascript method on a variable in mustache? Here's some sample code:

var playerTemplate = '<li><span class="player-position">{{ position }}</span><span class="player-name">{{ first_name }} {{ last_name }}</span></li>';
var playerRow = Mustache.to_html(playerTemplate, player);
$('ul#players-list').append(playerRow);

what i'd like to do is something like:

{{ position.toUpperCase() }}

I'd rather not change the object itself, because i may want {{ position }} to not be upper case in other situations. Any tips on the cleanest smartest way to do this?

Thanks much.

I have a simple mustache template setup that takes an object player and creates a list element. What's the best way to perform a javascript method on a variable in mustache? Here's some sample code:

var playerTemplate = '<li><span class="player-position">{{ position }}</span><span class="player-name">{{ first_name }} {{ last_name }}</span></li>';
var playerRow = Mustache.to_html(playerTemplate, player);
$('ul#players-list').append(playerRow);

what i'd like to do is something like:

{{ position.toUpperCase() }}

I'd rather not change the object itself, because i may want {{ position }} to not be upper case in other situations. Any tips on the cleanest smartest way to do this?

Thanks much.

Share Improve this question asked Dec 10, 2011 at 23:23 tuddytuddy 1,8344 gold badges31 silver badges36 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Mustache is intentionally very simple ("Logic-less templates") so there's not a lot you can do. One option is to add a positionUC property:

player.positionUC = player.position.toUpperCase();

and then in the template:

{{positionUC}}

You can also add functions:

// Note that render(text) will be HTML though...
player.uc = function(text, render) { return render(text).toUpperCase() };

and then in the template:

{{#uc}}{{position}}{{/uc}}

Perhaps you could switch to Handlebars and add a helper to uppercase things.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744702614a4588862.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信