# Variables

## Overview

Labii variables are dynamic placeholders that enable you to substitute specific values within parameters automatically. Variables streamline data management by allowing you to create reusable templates and configurations that adapt to different contexts without manual reconfiguration. This powerful feature enhances automation, reduces errors, and improves productivity in laboratory workflows.

{% hint style="info" %}
Variables are enclosed in double curly braces: `{{VARIABLE_NAME}}`. They are automatically replaced with their corresponding values when processed.
{% endhint %}

## Record Properties

Access properties of the current record using these variables:

| Variable                 | Description                                 | Example Output              |
| ------------------------ | ------------------------------------------- | --------------------------- |
| `{{SID}}`                | System identifier of the current record     | `abc123xyz`                 |
| `{{UID}}`                | Unique identifier of the current record     | `EXP-001`                   |
| `{{NAME}}`               | Name of the current record                  | `My Experiment`             |
| `{{DESCRIPTION}}`        | Description of the current record           | `Testing compound efficacy` |
| `{{PROJECTS}}`           | Projects associated with the current record | Project data array          |
| `{{OWNER.SID}}`          | System identifier of the record owner       | `user456`                   |
| `{{OWNER.NAME}}`         | Name of the record owner                    | `John Doe`                  |
| `{{DATECREATED}}`        | Date and time the record was created        | `2026-03-17 10:30:00`       |
| `{{VERSION}}`            | Version number of the current record        | `3`                         |
| `{{LINK}}`               | Direct link to the record's detail view     | Full URL to record          |
| `{{SHARABLELINK:<URL>}}` | Sharable link for external access           | Formatted sharable URL      |

{% hint style="warning" %}
For `{{SHARABLELINK:<URL>}}`, the URL must contain an `api_key` parameter to allow access by users without Labii authentication.
{% endhint %}

## URL Variables

Access information about the current page URL:

| Variable             | Description          | Example Output                  |
| -------------------- | -------------------- | ------------------------------- |
| `{{URL}}`            | Complete current URL | `https://labii.com/records/123` |
| `{{DOMAINFRONTEND}}` | Current domain       | `https://labii.com`             |

## Date Variables

Generate dynamic dates for time-sensitive operations:

### Current Date

* `{{TODAY}}` - Returns today's date in `YYYY-MM-DD` format
  * Example: `2026-03-17`

### Formatted Current Date/Time

* `{{CURRENT:<FORMAT>}}` - Returns current date/time with custom formatting
  * **JavaScript**: Use [Moment.js format](https://momentjs.com/) patterns
    * Example: `{{CURRENT:MMMM Do YYYY, h:mm:ss a}}` → `March 17th 2026, 2:30:45 pm`
  * **Python**: Use [Strftime format](https://strftime.org/) codes
    * Example: `{{CURRENT:%B %d, %Y}}` → `March 17, 2026`

### Relative Dates

Calculate dates relative to today using the pattern: `{{[+/-][#][DAY/WEEK/MONTH/YEAR]:<FORMAT>}}`

**Syntax:**

* `+` or `-`: Add or subtract time
* `#`: Number of units (1, 2, 3, etc.)
* Unit: `DAY`, `WEEK`, `MONTH`, or `YEAR`
* `<FORMAT>`: Optional format (defaults to `YYYY-MM-DD`)

**Examples:**

* `{{-5WEEK}}` - 5 weeks ago → `2026-02-10`
* `{{+30DAY}}` - 30 days from now → `2026-04-16`
* `{{-1YEAR:YYYY}}` - Year value from 1 year ago → `2025`
* `{{+2MONTH:MMM YYYY}}` - 2 months from now → `May 2026`

{% hint style="info" %}
**JavaScript**: Supports custom formatting using [Moment.js](https://momentjs.com/) patterns\
**Python**: Format support is currently not available for relative dates; returns `YYYY-MM-DD` format only
{% endhint %}

## Column Variables

Reference and process data from table columns:

### Basic Column Reference

* `{{[COLUMN_SID]}}` - Returns the value of a column using its SID
  * Example: `{{nqtw0a40x1d58puzEJOTY}}` returns the column's stored value

### Processed Column Values

* `{{[COLUMN_SID]:<CODE>}}` - Returns column value processed through custom code

#### JavaScript Processing

Execute any JavaScript code to transform column data. The column value is available as the `data` variable.

**Basic Operations:**

```javascript
{{nqtw0a40x1d58puzEJOTY:parseInt(data)}}          // Convert to integer
{{column_sid:parseFloat(data)}}                   // Convert to float
{{column_sid:data.toUpperCase()}}                 // Convert to uppercase
{{column_sid:data * 2}}                           // Multiply by 2
{{column_sid:data.length}}                        // Get string/array length
```

**Built-in Statistical Functions:**

| Function              | Description                    | Usage Example                              |
| --------------------- | ------------------------------ | ------------------------------------------ |
| `sum`                 | Calculate sum of number array  | `{{column_sid:sum(data)}}`                 |
| `mean`                | Calculate mean (average)       | `{{column_sid:mean(data)}}`                |
| `se`                  | Calculate standard error       | `{{column_sid:se(data)}}`                  |
| `sd`                  | Calculate standard deviation   | `{{column_sid:sd(data)}}`                  |
| `encodeStringForURL`  | URL-encode a string            | `{{column_sid:encodeStringForURL(data)}}`  |
| `decodeStringFromURL` | URL-decode a string            | `{{column_sid:decodeStringFromURL(data)}}` |
| `Labii`               | Access Labii utility functions | `{{column_sid:Labii.functionName(data)}}`  |

**Complex Examples:**

```javascript
// Calculate average and round to 2 decimal places
{{column_sid:mean(data).toFixed(2)}}

// Get sum and format with units
{{column_sid:sum(data) + ' mg'}}

// Conditional formatting
{{column_sid:data > 100 ? 'High' : 'Low'}}
```

#### Python Processing

Access nested dictionary values using dot notation to specify keys:

**Syntax:** `{{[COLUMN_SID]:key1.key2.key3}}`

**Examples:**

```python
{{nqtw0a40x1d58puzEJOTY:user.sid}}        // Access user.sid from column data
{{column_sid:metadata.created_by}}        // Access nested created_by field
{{column_sid:results.concentration}}      // Access concentration in results
```

{% hint style="info" %}
Python code processing is limited to dictionary key access only. Full Python expressions are not supported.
{% endhint %}

## User Variables

Access information about the current logged-in user:

| Variable        | Description                       | Example Output |
| --------------- | --------------------------------- | -------------- |
| `{{USER.SID}}`  | System identifier of current user | `user789`      |
| `{{USER.NAME}}` | Full name of current user         | `Jane Smith`   |

## Metadata Variables

Use metadata labels as variables to dynamically reference organization-level or project-level settings:

**Syntax:** Define metadata with labels wrapped in `{{...}}`, then reference them as variables.

**Example:**

```json
{
  "label": "{{API_KEY}}",
  "value": "sk_live_abc123xyz789"
}
```

Once defined, use `{{API_KEY}}` anywhere to substitute the value automatically.

**Common Use Cases:**

* Store shared API keys for generating sharable links
* Define organization-wide constants
* Maintain environment-specific configuration values
* Reference project-specific parameters

{% hint style="success" %}
**Best Practice:** Store sensitive values like API keys as metadata at the organization level, then reference them using variables. This centralizes management and improves security.
{% endhint %}

## Escaping Variables

To prevent a string from being interpreted as a variable, escape the curly braces with backslashes:

**Escaped Format:** `\\{\\{string\\}\\}`

**Example:**

* Without escaping: `{{TEST}}` → Replaced with TEST variable value
* With escaping: `\\{\\{TEST\\}\\}` → Displays as `{{TEST}}` (literal text)

{% hint style="info" %}
Use escaping when you need to display variable syntax as literal text in documentation or when the curly braces are part of your actual data format.
{% endhint %}

## Usage Examples

### Example 1: Dynamic Record Title

```
Experiment {{UID}} - {{NAME}} (Version {{VERSION}})
```

Output: `Experiment EXP-001 - Compound Testing (Version 3)`

### Example 2: Date Range Query

```
Records from {{-30DAY}} to {{TODAY}}
```

Output: `Records from 2026-02-15 to 2026-03-17`

### Example 3: Sharable Report Link

```
{{SHARABLELINK:{{DOMAINFRONTEND}}/api/records/{{SID}}/?api_key={{API_KEY}}}}
```

Output: Complete sharable URL with authentication

### Example 4: Statistical Summary

```
Mean Concentration: {{concentration_column_sid:mean(data).toFixed(2)}} μM
Standard Deviation: {{concentration_column_sid:sd(data).toFixed(2)}} μM
```

Output: `Mean Concentration: 45.67 μM, Standard Deviation: 3.21 μM`
