|
General Variable Types
The Discus Template Language comes with several variable types that function similarly to their Perl counterparts. Among these are:
- Scalars
- Arrays
- Hashes
- Hash references
Variables are generally identified for substitution by the $ character. The following types of variables are supported:
Scalars: $x
The most simple scalar substitution is made with a $ followed by an alphanumeric variable name. The value of this variable is determined by returning the earliest of the following that is defined:
- The variable as defined with a <#define ...#> statement
- The referenced discus.conf variable ($x is equivalent to $DCONF->{x})
- The referenced parameter ($x is equivalent to $PARAMS->{x})
Although it is easier to use this notation to access discus.conf variables, we suggest using $DCONF->{...} or $PARAMS->{...} if that is what you intend.
$DCONF->{admin_dir} and $admin_dir are generally the same
Values in hashes: $x->{y}
The most common variable type used in Discus templates, this notation extracts a value from the hash x given by the key y.
Your name is $profile->{fullname}
It is also possible to extract a value from a hash, where the key itself is given by either a variable or the value from another hash.
$general->{$test} $general->{$hash2->{key2}}
Finally, it is possible to extract a value from a hash that is itself stored within another hash:
$general->{key1}->{key2}
Elements from arrays: $x[...]
It is possible to extract individual elements from arrays.
The index (part within the brackets) is a number 1 or greater that describes which element of an array is to be taken. Thus, $x[1] takes the first element of the array and $x[2] takes the second element from that array. Note that Discus arrays are referenced with a first array element of 1, so $x[0] does not exist. Discus arrays are referenced with the first index being 1, which is different from Perl but much more convenient in processing of the types of data used by a discussion board application.
# $general->{one} is 1
$x[$general->{one}] is the first element of the array
Generally, the elements of arrays used within Discus are themselves hashes, making the most common construct one that uses the element itself as a hash and then extracts a value corresponding to a particular key.
# $general->{one} is 1
First name in array: $x[$general->{one}]->{name}
Another way of getting it: $x[1]->{name}
Size of arrays: $#x
The size of an array (number of elements in that array) is given by the notation $#x, where x is the name of the array. Note that because Discus arrays are referenced from an initial index of 1, whereas Perl arrays are referenced from an initial index of 0, the notation $#x will be one greater than the corresponding Perl expression. In this sense, the notation $#x is equivalent to the Perl statement of scalar(@x).
$#x returns the number of elements in the array x
|