ZPT-JS reference - Attributes - TALDeclare

Syntax

argument       ::= declare_item [';' declare_item]*
declare_item   ::= ('required') variable_name (variable_type (expression))
variable_name  ::= Name
variable_type  ::= Name
                

Description

The data-declare statement declares a list of variables and some settings about them.

Each declaration can define:

Some example of types:

Type Value example
number NaN
number 5
array []
string 'any string'
function function(){}
regexp /a/
date new Date()
object {}
myconstructor new MyConstructor()

Any value matches undefined or null types.

A strict mode is set to true inside the node with this attribute. Strict mode makes it easy to force to declare variables. If strict mode is true and ZPT-JS finds a not declared variable an error occurs. Strict mode can be defined in two ways:

An error occurs if:

If an error occurs ZPT-JS stop processing the nodes and show the error (using a javascript alert by default). ZPT-JS uses the errorFunction defined in context. To customize it use the setErrorFunction in context.

Differences with ZPT

This statement does not exist in ZPT.

Examples

Declaring a number, a string, an array and a date:

data-declare="aNumber number;
              aText string;
              anArray array;
              aDateValue date"
                

Declaring a number, a string, an array and a date (all required):

data-declare="required aNumber number;
              required aText string;
              required anArray array;
              required aDateValue date"
                

Declaring a required variable (no type):

data-declare="required myVar"
                

Declaring some default values:

data-declare="aNumber number 99;
              aText string 'a default value'"