listFormat: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
m (Applied Template:MacroFunction)
No edit summary
 
(6 intermediate revisions by 3 users not shown)
Line 5: Line 5:


|usage=
|usage=
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[ listFormat(list, listFormat, itemFormat, separator) ]
[ listFormat(list, listFormat, itemFormat, separator) ]
[ listFormat(list, listFormat, itemFormat, separator, delim) ]
[ listFormat(list, listFormat, itemFormat, separator, delim) ]
</source>
</syntaxhighlight>
* listFormat is a string that is emitted once. It should contain the text "%list", which is replaced with the formatted items.
* listFormat is a string that is emitted once. It should contain the text "%list", which is replaced with the formatted items.
* itemFormat is emitted once per item. Each instance of "%item" in the string is replaced with the value of the list item.
* itemFormat is emitted once per item. Each instance of "%item" in the string is replaced with the value of the list item.
Line 14: Line 14:


|example=
|example=
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[ listFormat("apple,bear,cat", "BEGIN LIST<br>%list<br>END LIST", "This item is: %item", "<br>") ]
[ listFormat("apple,bear,cat", "BEGIN LIST<br>%list<br>END LIST", "This item is: %item", "<br>") ]
</source>
</syntaxhighlight>
(prints items on separate lines)
(prints items on separate lines)
Convert a string list to html list:<syntaxhighlight lang="mtmacro" line>
[R: listFormat( "apple, bear, cat", "<ul>%list</ul>", "<li>%item</li>", "" ) ]
</syntaxhighlight>
Produces:<ul>
<li>apple</li>
<li>bear</li>
<li>cat</li>
</ul>
Create an option list input (drop-down list selection) for an html form, with the names of selected tokens:
<syntaxhighlight lang="mtmacro" line>
[R: listFormat( getSelectedNames( "%%" ),
    "<select name='test'>%list</select>",
    "<option value='%item'>%item</option>",
    "", 
    "%%" )
]
</syntaxhighlight>
The first argument is the list, returned by {{func|getSelectedNames}}: it has a delimiter specified ("%%"), to avoid PC names with commas or anything other than "%%" from appearing as more than one item.  The second argument specifies html (text) to go around the entire formatted ''list'', and the third is html to wrap around each ''item'' in the list.  The fourth argument is blank (empty), since no separator between items is needed in this case.  The fifth argument is usually optional, but in this case is the same delimiter specified in getSelectedNames(), to allow listFormat to find each item in the list.
}}
}}
[[Category:String List Function]]
[[Category:String List Function]]

Latest revision as of 17:50, 15 March 2023

listFormat() Function

Returns a custom-formatted version of the list.

Usage

[ listFormat(list, listFormat, itemFormat, separator) ]
[ listFormat(list, listFormat, itemFormat, separator, delim) ]
  • listFormat is a string that is emitted once. It should contain the text "%list", which is replaced with the formatted items.
  • itemFormat is emitted once per item. Each instance of "%item" in the string is replaced with the value of the list item.
  • separator is emitted in between the formatted items.

Example

[ listFormat("apple,bear,cat", "BEGIN LIST<br>%list<br>END LIST", "This item is: %item", "<br>") ]

(prints items on separate lines)

Convert a string list to html list:
[R: listFormat( "apple, bear, cat", "<ul>%list</ul>", "<li>%item</li>", "" ) ]
Produces:
  • apple
  • bear
  • cat


Create an option list input (drop-down list selection) for an html form, with the names of selected tokens:

[R: listFormat( getSelectedNames( "%%" ), 
    "<select name='test'>%list</select>", 
    "<option value='%item'>%item</option>", 
    "",  
    "%%" ) 
]
The first argument is the list, returned by getSelectedNames(): it has a delimiter specified ("%%"), to avoid PC names with commas or anything other than "%%" from appearing as more than one item. The second argument specifies html (text) to go around the entire formatted list, and the third is html to wrap around each item in the list. The fourth argument is blank (empty), since no separator between items is needed in this case. The fifth argument is usually optional, but in this case is the same delimiter specified in getSelectedNames(), to allow listFormat to find each item in the list.