Skip to the content
Documentation

def

No ratings yet

Summary

Declares (and defines) a variable. Warns if the variable already exists.

Usage

{def $var1=value1 [ $var2=value2 [...] ]}

Parameters

Name Type Description Required
$var1 string Name of variable number one (with a dollar sign in front of it). Yes.
value1 any The value that should be assigned to variable one. Yes.
$var2 string Name of variable number two (with a dollar sign in front of it). No.
value2 any The value that should be assigned to variable two. No.

Description

This function allows the declaration and definition of a single variable or a group of variables. The "undef" function can be used to flush/destroy variables that were created using the "def" function. Please note that this function does not support the "name" and the "scope" parameters (like the old {let} did).

Replacement for "default"

The following technique can be used as a replacement for the old "default" function:

{if is_set( $a )|not}
  {def $a=5}

Examples

Example 1

{def $oranges=13}

This example demonstrates how the "def" function can be used to declare a variable called "oranges". The variable will be declared as an integer with a value of 13.

Example 2

{def $oranges=13 $apples='There are no apples.'}

or

{def $oranges=13
     $apples='There are no apples.'}

These code snippets demonstrates how the "def" function can be used to declare multiple variables. A variable called "oranges" will be declared as an integer with a value of 13. A variable called "apples" will be declared as a string containing the following characters: "There are no apples.".

Contributors

Comments (2 historical)

Written by readers of the earlier documentation sites, kept as they were.

  1. Replacement for "default" not correct

    Dirk ·

    The ending if tag is missing. So it should be like this:

    {if is_set( $a )|not}
      {def $a=5}
    {/if}
  2. Schemers

    Miguel Pérez ·

    def seems to be similar to let* in Scheme. This means you can do things such as
    {def $a=1 $b=$a}
    and $b would get bound to 1 as well.

Comments

Add a comment

Comments are read by a moderator before they appear.