4.2 ALIAS

Provides a way to create constant value that can be used later in the template. When the alias name is encountered later in the template, it will be expanded to the constant value assigned to it. Using ALIAS is more efficient than using template variables created with the SET keyword because ALIAS names are expanded when the template is initially compiled rather than when it is processed during a request. ALIAS essentially provides a shortcut notation within templates. When an ALIAS name is used, it must be between squiggly braces {} or it will not be expanded.

Example:

{ALIAS ITEMS_PER_PAGE 10}
{ALIAS ITEMS_REMAINING {ITEMS_PER_PAGE} - count}
{SET count = 1}
{WHILE count <= {ITEMS_PER_PAGE}}
  {SET remaining = {ITEMS_REMAINING}}
  This is item number {VAR count}. There are {VAR remaining} remaining.<BR>
  {INC count}
{ENDWHILE}

The preceding template produces the following output:

This is item number 1. There are 9 remaining.
This is item number 2. There are 8 remaining.
This is item number 3. There are 7 remaining.
This is item number 4. There are 6 remaining.
This is item number 5. There are 5 remaining.
This is item number 6. There are 4 remaining.
This is item number 7. There are 3 remaining.
This is item number 8. There are 2 remaining.
This is item number 9. There are 1 remaining.
This is item number 10. There are 0 remaining.

Note that if we had attempted to use {VAR {ITEMS_REMAINING}}, we would have encountered a compile error similar to the following:

Compile Error: aliastest.htt: Line 45: Invalid name for a variable: 10. Cannot load file: aliastest.htt.

The reference to {VAR {ITEMS_REMAINING}} does not work because {ITEMS_REMAINING} is not a valid variable name. It expands to {ITEMS_PER_PAGE} - count, ITEMS_PER_PAGE expands to 10, and the result is 10 - count. Because the VAR keyword expects the name of a variable, and 10 is not a valid variable name, the error occurs.

Sometimes template data is provided in the form of numbers, making templates difficult to read and maintain. In this situation, you can use the ALIAS keyword to make your templates more readable. For example, suppose a variable named Object.type is returned, and its possible values are numeric:

{ALIAS TYPE_USER 1}
{ALIAS TYPE_GROUP 2}
.
.
.
{IF Object.type == {TYPE_USER}}
    {! Show information about the user object}
    {TYPE_USER} is a user object.
{ELSEIF Object.type == {TYPE_GROUP}}
    {! Show information about the group object}
    {TYPE_GROUP} is a group object.
{ENDIF}