模板:Trunc/doc
此页面是Template:Trunc的模板文档。 此页面可能包含了模板的使用说明、分类和其他内容。 |
此模板文档被引用于系统界面中。 任何对此模板文档的修改会立即反映于维基百科的用户界面。 为了避免造成大规模的影响,编者可以在本模板的沙盒 和测试样例 页面进行实验。2 测试后无误的版本可以一次性地加入此模板文档中,但是修改前请务必于讨论页发起讨论。 |
Usage
[编辑]{{Str left|<string>|<count>}}
Gives the resultant <count> of characters creating a substring of characters from the start of the trimmed string (i.e. the substring returned will have length <count>, exclusive of leading whitespace characters, which are trimmed first before <count> is invoked).
If <count> is invalid, empty or zero, an empty string is returned. If undefined, it defaults to 1.
Limitations
[编辑]- Length to 500: The maximum substring stops at 500 long, yet gives no error message. Only 500 characters can be extracted even if the <string> is longer.
- Inexpensive (uses no other templates).
Examples
[编辑]{{ Trunc | Lorem ipsum dolor sit amet | 10 }}
→ Lorem ipsu{{Str left| Lorem ipsum dolor sit amet | 4 }}
→ Lore, (note leading spaces trimmed!){{ Trunc | Lorem ipsum dolor sit amet | 1 }}
→ L{{ Trunc | Lorem ipsum dolor sit amet | 0 }}
→{{ Trunc | Lorem ipsum dolor sit amet | }}
→ Lorem ipsum dolor sit amet{{ Trunc | Lorem ipsum dolor sit amet }}
→ Lorem ipsum dolor sit amet{{ Trunc | Lorem ipsum dolor sit amet | 40 }}
→ Lorem ipsum dolor sit ametLorem ipsum do{{ Trunc | àçé | 2 }}
→ àç- This example is demonstrating the safety of this template with the UTF-8 encoding (this should work now) :
Examples of limitations
[编辑]All the limitations of use for this template and shown below are caused by the current implementation of the {{padleft:}} parser function used in this template, which does not filter its third parameter containing the characters used for padding a string to the specified length (it currently counts incorrectly the characters to extract from the padding string, and incorrectly measures its effective length; in addition this parameter is limited to 500 bytes).
The following examples are demonstrating that this template does not interpret character entity references as the character they represent. It treats them as multiple characters and can leave them truncated.
{{ Trunc | a c | 3 }}
→ a&n- The output "a c" may have been intended, but the named character entity was truncated, leaving no length available for the final "c".
{{ Trunc | aBc | 3 }}
→ a&#- This should display "abc", but the named character entity was truncated, leaving no length available for the final "c".
{{ Trunc | a c | 3 }}
→ a c- The result is correct, as expected.
{{ Trunc | a c | 3 }}
→ a&#- This last example should be equivalent to the previous one, but it is not.
This template will erase nowiki tags without counting their length:
{{ Trunc | a<nowiki/>bcd | 3 }}
→ abc- Returns three characters as expected
{{ Trunc | a<nowiki>E</nowiki>bcd | 3 }}
→ abc- Returns three characters, but not aEb as might have been expected.
{{ Trunc | a<nowiki/>bc | 10 }}
→ abcabcabca- Returns 10 characters as expected.
HTML comments, and the "noinclude", "includeonly" and "onlyinclude" wiki markup tags are also ignored, because they are preprocessed in template parameters, before including it and substituting parameters:
{{ Trunc | a<noinclude/>bc | 3 }}
→ abc{{ Trunc | a<!-- comment -->bc | 3 }}
→ abc
Note also that the specified length will cause the input string to be padded by repeating it, if this length is larger than the input string length:
{{ Trunc | Abc. | 10 }}
→ Abc.Abc.Ab
Template data
[编辑]A template to give the <count> substring of characters from the start of the trimmed string
参数 | 描述 | 类型 | 状态 | |
---|---|---|---|---|
String | 1 | The string to be trimmed and counted | 字符串 | 必需 |
Count | 2 | Gives the <count> substring of characters from the start of the trimmed string | 数字 | 必需 |
See also
[编辑]- Bugzilla:22555 (historical; need for correcting padleft/padright functions and to provide better string-handling parser functions)