> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ubiquex.io/llms.txt
> Use this file to discover all available pages before exploring further.

# locals

> The locals block defines intermediate computed values within a stack.

The `locals` block defines named computed values that can be referenced by resources and outputs within the same stack. Locals are the right place for conditional expressions and other transformations that cannot appear directly in resource properties.

## Syntax

```xcl theme={"theme":"css-variables"}
locals "name" {
    field_name = expr
}
```

## Example

```xcl theme={"theme":"css-variables"}
locals "networking" {
    nat_tier = "standard" if input.deploy_nat else "none"
}
```

Generated Python:

```python theme={"theme":"css-variables"}
# ── Locals ────────────────────────────────────────────────────────────────────
nat_tier = ("standard" if deploy_nat else "none")
```

## Referencing locals

Locals fields are accessed via the `locals.fieldName` qualified form:

```xcl theme={"theme":"css-variables"}
stack "s" {
    resource = SomeProvider.Resource {
        tier = locals.nat_tier
    }
}
```

With multiple locals blocks, use `locals.blockName.fieldName`:

```xcl theme={"theme":"css-variables"}
locals "computed" {
    arn_prefix = f"arn:aws:s3:::{input.bucket_name}"
}

// reference:
output "s" {
    arn = locals.computed.arn_prefix
}
```

## Why locals exist

Resource properties cannot contain `match` or ternary (`if/else`) expressions directly — those must go in `locals`:

```xcl theme={"theme":"css-variables"}
// Error — conditional in resource property:
stack "s" {
    bucket = aws.s3.Bucket {
        acl = "public" if input.public else "private"  // not allowed
    }
}

// Correct:
locals "s" {
    acl = "public" if input.public else "private"
}

stack "s" {
    bucket = aws.s3.Bucket {
        acl = locals.acl
    }
}
```

## Supported expressions

Locals can hold any expression:

* Ternary: `"a" if cond else "b"`
* Match: `match input.env { "prod": "t3.large"  default: "t3.micro" }`
* F-strings: `f"prefix-{input.name}-suffix"`
* Arithmetic: `input.count * 2`
* Boolean logic: `input.enabled && !input.disabled`
* Cross-stack refs: `@otherStack.someOutput`

## Reserved field names

The same reserved names that apply to `input` also apply to `locals`:

`stack`, `input`, `output`, `locals`, `module`, `provider`, `import`, `space`, `root`, `for`, `when`, `match`, `default`, `count`, `each`

## Duplicate field names

Locals field names are unique across all `locals` blocks in the stack. Duplicates produce a compile error.
