AMP Mustache and Math

◇◆丶佛笑我妖孽 提交于 2019-12-23 02:22:09

问题


I am getting some variables from a JSON file using amp-list and amp-template. One of the variables I've gathered is a number that I need to round up or down. The number would normally be displayed as {{number}}. However, I need to manipulate this number to make it a whole number. I am not sure how to do the conversion on that amp var. any help is appreciated.


回答1:


If you're displaying data in an amp-list you can do the rounding inside the src attribute by using amp-state and map:

<amp-state id="myState" src="http://your-data.com/json">

<amp-list 
    ... 
    [src]="
        myState.myItems.map(item => {
            roundedValue: round(item.value),
            otherProperty: item.otherProperty
        })
    "> 
    ... 
</amp-list>

and then use roundedValue in your template.

See this page for the list of supported functions:

https://www.ampproject.org/es/docs/reference/components/amp-bind#white-listed-functions

But keep in mind that if your objects have a lot of fields you may run into the expression size limit that AMP imposes on expressions inside attributes (they can't perform more than 50 operations IIRC, including function calls, math operations, field dereferencing, etc).




回答2:


Mustache is a Logicless template so you can't do any math with it, all your data have to be prepared elsewhere before it has reached a mustache. More info here https://mustache.github.io/mustache.5.html



来源:https://stackoverflow.com/questions/49302139/amp-mustache-and-math

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!