官术网_书友最值得收藏!

Passing in a second variable

The alternative to the preceding autodetection is to pass the second variable into the format function. This gives us greater flexibility and scalability should we wish to format other fields. With the second variable, we can either pass in a fixed string that matches a preselected list in our switch statement or we could pass in the field itself. An example of the fixed string approach in the view would be:

      {{ format(person.balance, 'currency') }}

This would work perfectly and would be great if we had several different fields that all needed to be formatted like balance currently does, but there seems to be some slight repetition in using the balance key and currency format. 

As a compromise, we are going to pass the person object as the first parameter, so we can access all the data, and the name of the field as the second parameter. We will then use this for both identifying the format method required and to return the specific data.

主站蜘蛛池模板: 关岭| 武城县| 大兴区| 鹤岗市| 湘西| 韶山市| 漠河县| 英吉沙县| 阿拉善盟| 昌邑市| 库尔勒市| 南靖县| 基隆市| 茌平县| 广河县| 池州市| 阳江市| 台中县| 牡丹江市| 五家渠市| 临城县| 名山县| 清原| 格尔木市| 健康| 怀安县| 鸡西市| 阳信县| 广水市| 宁明县| 富源县| 科技| 辽阳市| 兴和县| 五寨县| 北京市| 岚皋县| 绥江县| 平湖市| 呼玛县| 临汾市|