Http://extjs.com/learn/Tutorial:Utilizing_Format_Features_of_Templates)
Suppose we want to print the content from a variable, but the content may occupy too much space. In this case, you can intercept the content within 50 English characters,
Make a connection and let the user click to view the full text. Function "ellipsis"
In this way, it can be limited to any number of characters. In addition, after the string is intercepted, the function will add "..." to show more actual content.
A template is shown as follows:
VaR mytpl = new ext
. Template (
'<Div >{ content: ellipsis (50)} <br/> <a href = "{morelink}"> Read More </a> </div>'
);
After processing, 47 characters belong to the content, and the other three characters are "...", with a total of 50 characters.
This is a list of functions that can be used for template formatting:
* Ellipsis (length)-crop a string greater than the specified length to display the ellipsis. Applicable to displaying only the first n characters, and then providing detailed page links.
* UNDEF-check whether a value is underfined. If yes, convert it to a null value.
* Htmlencode-conversion (&, <,>, and ') characters
* Trim-Trim unnecessary spaces before and after a piece of text
* Substr (START, length)-returns a substring of the specified length starting from the specified position.
* Lowercase-returns a string in which letters are converted to lowercase letters.
* Uppercase-returns a string in which letters are converted to uppercase letters.
* Capitalize-returns a string in which the first letter is converted to an upper-case letter and the remaining one is lower-case.
* Usmoney-format the number to the dollar currency. Example: $10.97
* Date [(Format)]-parses a date into a date in a specific format mode. If the date string is not input, the default value is "month/day/year"
* Striptags-strip all HTML tags of Variables
You can also create a custom Formatting Function. Specifically, add a new method to the template instance and call it on the template. The formatting function should be like this: {Variable: This. <formatfunction> </formatfunction> }"
This is a simple instance, adding a new function "parsejson" to the template instance.
JS Code
- VaR
Tplfun =
New
EXT
. Template (
- '<Table border = 1 cellpadding = 0 cellspacing = 0>'
,
- '<Tr> <TD width = 90> name </TD>'
,
- '<TD width = 120 >{name} </TD> </tr>'
,
- '<Tr> <TD width = 90> age </TD>'
,
- '<TD width = 120 >{age} </TD> </tr>'
,
- '<Tr> <TD width = 90> height </TD>'
,
- '<TD width = 120 >{stature: This. parsejson ()} </TD> </tr>'
,
- '</Table>'
- );
- VaR
Data = {
- Name: 'Tom'
,
- Age: 24,
- Stature :{
- Num: 170,
- Unit: 'centimeter'
- }
- }
- Tplfun. parsejson = Function
(JSON ){
- Return
JSON. Num + JSON. Unit;
- }
- Tplfun. append ('tpl-table'
, Data );