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

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.

主站蜘蛛池模板: 英山县| 沙湾县| 西青区| 改则县| 格尔木市| 宁陵县| 望城县| 中方县| 徐汇区| 宝鸡市| 天等县| 龙胜| 巴东县| 渑池县| 刚察县| 商丘市| 日喀则市| 乌拉特后旗| 姜堰市| 益阳市| 遵义市| 五寨县| 岳阳县| 临朐县| 儋州市| 普宁市| 绥德县| 巴楚县| 莱州市| 唐海县| 长寿区| 仪陇县| 同心县| 绥芬河市| 梨树县| 剑河县| 定西市| 门头沟区| 叙永县| 阿拉善盟| 北安市|