[Baidu amis] Template
Template
In order to render text and data structures more flexibly, amis implements two template rendering functions refer to other template engines.
-
Template string
${xxx}
-
JavaScript template engine
<%= data.xxx %>
,<%- expression %>
Template string
Plain text
Configure plain text and output.
1 | Hello World! |
1 | { |
Use variables in text
It can support the use of Data Mapping - https://baidu.github.io/amis/zh-CN/docs/concepts/data-mapping syntax in plain text: ${xxx}
to get the value of the variable in the data
attribute, as follows:
1 | Hello World! |
1 | { |
For more ${xxx}
syntax related introduction, move to Data Mapping.
Render HTML
Use Data Mapping syntax: ${xxx}
to get the value of the variable in the data
attribute and render HTML
1 | Hello |
1 | { |
JavaScript template engine
amis also supports the use of JavaScript template engine for organizing output, and the internal use of lodash - https://lodash.com/docs/4.17.15#template template for implementation.
1 | User: no one |
1 | { |
Did you notice?
In the JavaScript template engine, the way we get the data
attribute variables is data.xxx
, not the previous ${xxx}
. If you are familiar with JavaScript, the template engine here actually treats the data
attribute as the data scope of the current code. Execute, so you need to use data.xxx
for value
Pay attention to the correct value method in different scenarios when using the template.
Looking carefully at the example, it is not difficult to find that the syntax is very similar to EJS – Embedded JavaScript templates - https://ejs.co/, <% here is the js statement %>
, so as long as you can write js, there is no problem with page rendering. In addition, the following are some available js methods.
-
formatDate(value, format='LLL', inputFormat='')
format the time format. For format, please go to the moment documentation page Moment.js | Docs - https://momentjs.com/docs/. -
formatTimeStamp(value, format='LLL')
Format the time stamp as a string. -
formatNumber(number)
Format the number format, plus thousands. -
countDown(value)
Countdown, showing how many days are left before the specified time, only supports timestamp.
The following methods in filters can also be used such as: <%= date(data.xxx,'YYYY-MM-DD') %>
Limititaions
1. Template strings and template engines cannot be used interchangeably
E.g:
1 | { |
References
[1] Template | amis - 低代码前端框架 - https://baidu.github.io/amis/zh-CN/docs/concepts/template
[2] Data Mapping | amis - 低代码前端框架 - https://baidu.github.io/amis/zh-CN/docs/concepts/data-mapping
[3] Template | Lodash Documentation - https://lodash.com/docs/4.17.15#template
[4] EJS – Embedded JavaScript templates - https://ejs.co/
[5] Moment.js | Docs - https://momentjs.com/docs/
[6] baidu/amis: 前端低代码框架,通过 JSON 配置就能生成各种页面。 - https://github.com/baidu/amis