Sunday, July 18, 2010

ComponentSoft TemplateEngine - Creating a new Array

UltimateTemplateEngine allows you to declare and initialize an array like:
Copy Code

{%arr = {1,2,3,"abc"}/}

{! arr is an array containing four items 1,2,3 and "abc" !}

{%foreach item in arr%}

$item$
javascript:void(0)
{$foreach%}

The output would be:

1

2

3

abc

The array created above is a fixed array and you cannot add more element to it.

To create a fixed array without initialization in your template code, you can call NewFixedArray function returning a reference to a newly created fixed array. See the following example for more details:
Copy Code

{%arr = NewFixedArray(typeof("char"), 5)/}

{! arr is a fixed array of five characters !}

If you wish to create a dynamic array for further manipulation, UltimateTemplateEngine provides another easy declaration for dynamic array, as shown below:
Copy Code

{%dynarr = {}/}

{%dynarr.Add("123");dynarr.Add("abc");/}

{! dynarr is a dynamic array containing two elements 123 and abc !}

With the array declared above you can add, remove elements to/from it.

You can also use indexer operator to assign a value to a specific element in the array. The indexer operator is applied for both fixed array and dynamic array:
Copy Code

{%dynarr = {};dynarr.Add(10)/}

{%arr = {1,2,3}/}

$dynarr[0]$ {! Print out the value at index 0 of the dynamic array !}

$arr[0]$ {! Print out the value at index 0 of the fixed array !}

{%dynarr[0] = 1/} {! Assignment !}

{%arr[0] = 11/} {! Assignment !}

No comments:

Post a Comment