Extension:ArrayFunctions/Essentials/Creating an array
Arrays can be created in two ways, either directly in wikitext or through Lua. In this chapter, we will focus on creating arrays using wikitext. For instructions on creating arrays using Lua with Scribunto, please refer to this chapter.
Lists and objects
editArrayFunctions provides two fundamental parser functions for creating arrays:
#af_list
: creates a list.#af_object
: creates an object.
We distinguish between lists and objects. Both are referred to as arrays, but are used slightly differently:
- A list is an ordered collection of items with a numeric index.
- An object is an unordered collection of items with a string index.
Using these parser functions is fairly straightforward. Below are examples of how to use each:
- Creating a list
{{#af_list: item1 | item2 | item3 }}
- Creating an object
{{#af_object: key1 = item1 | key2 = item2 | key3 = item3 }}
Displaying an array
editIf you tried one of the examples above, you might have been puzzled by the result. Instead of a nicely formatted array, you see an unreadable string. This happens because ArrayFunctions encodes the array before outputting it. This encoding is done for two main reasons:
- The encoded string will never be touched by MediaWiki.
- It creates an opaque type, ensuring all computation and manipulations happen through ArrayFunctions. This approach allows for greater flexibility in how values are internally managed and updated.
To display the contents of an array, you need to use additional ArrayFunctions functions designed to decode and output the data. Here is an example of how you can display the contents of a list or object:
{{#af_print: {{#af_list: item1 | item2 | item3 }} }} {{#af_print: {{#af_object: key1 = item1 | key2 = item2 | key3 = item3 }} }}
The #af_print
parser function is designed to output an array for debugging purposes only.