var template = "multi: {{#answerVOs}}{{answer}}; {{/answerVOs}}";
//需要=>多:选项1;选项2;option3(末尾没有分号和空格)
var json = {
"answerVOs": [
{ "answer" : "option 1"
},
{ "answer" : "option 2"
},
{ "answer" : "option 3"
}
]
};
document.getElementById("answers").innerHTML = Mustache.to_html(template, json);
http://jsfiddle.net/casadev/GEbe8/1/
我需要在没有分号和空格的情况下显示数组中的最后一项,任何想法。
编辑:PHP代码
class ChoiceVO extends \ValueObject {
public $id;
public $question;
public $answer;
public $answerformat;
public $fraction;
public $feedback;
public $feedbackformat;
public $replace = "function () { var result = ''; for( var i = 0; i < this.answers.length; i++ ) { result += this.answers[i].answer + (i !== this.answers.length - 1 ? '; ' : '');}return result;}";
function __construct($args=null) {
parent::__construct($args);
}
}