0
我有一個標籤,用於構建一個個人屬性字符串 - 身高,頭髮顏色,眼睛顏色和血型。所有這些字段對用戶來說都是可選的。最終的輸出字符串像:grails中更有效的字符串連接
棕色的頭髮,綠色的眼睛,5'5" ,A +型血
但由於字段是可選的輸出可能是:
綠色的眼睛
或
棕色的頭髮,5'5"
另一個扳手如果一個人是光頭,他們沒有頭髮,所以輸出爲:
禿,藍眼睛
我現在的標籤是這樣的:
def bodyStats = { attrs, body ->
def applicant = attrs.applicant
String stats = ''
if (applicant.hairColor) {
//if the applicant has hair, the output is <color> hair
if (!HairColor.BALD.equals(applicant.hairColor)) {
stats += g.message(code:'profile.hairColor', args:[applicant.hairColor])
}
//else it's just bald
else {
stats += applicant.hairColor
}
}
if (applicant.eyeColor) {
stats = stats ? (stats + ', ') : stats
stats += g.message(code:'profile.eyeColor', args:[applicant.hairColor])
}
if (applicant.heightFeet) {
stats = stats ? (stats + ', ') : stats
stats += g.message(code:'profile.height', args:[applicant.heightFeet, applicant.heightInches])
}
if (applicant.bloodType) {
stats = stats ? (stats + ', ') : stats
stats += g.message(code:'profile.bloodType', args:[applicant.heightFeet, applicant.heightInches])
}
out << stats
}
對於國際化,我在拉東西像「頭髮」和「眼睛」從messages.properties文件。
這是我能做的最好的嗎?如果有前面的屬性,我還必須考慮每個屬性之間的逗號。
另一個問題是它屬於一個標記,還是應該建立這個'屬性'字符串屬於域對象本身。
我認爲這種問題取決於個人意見,所以它適合在用戶郵件列表中更好。從常見問題解答:「並不是所有的問題都能很好地適用於我們的格式,避免主要基於意見的問題,或者可能產生討論而不是回答的問題。」 –