# Dynamic Expressions

### How to use variables in layouts ? <a href="#how-to-use-variables-in-layouts" id="how-to-use-variables-in-layouts"></a>

You must always precede the name of your variable with "page.".

```html
<div>Your name {{ page.variables }}</div>
```

### Conditionals <a href="#conditionals" id="conditionals"></a>

```html
<if condition="page.firstname">
  <div>Hello {{ page.firstname }}</div>
</if>
```

```html
<if condition="page.firstname === 'John'">
  <div>Hello John</div>
</if>
<elseif condition="page.firstname === 'Joe'">
  <div>Hello Joe</div>
</elseif>
<else>
    <div>Hello</div>
</else>
```

### Loops <a href="#loops" id="loops"></a>

#### Arrays <a href="#arrays" id="arrays"></a>

```html
<each loop="item, index in page.myArray">
  <p>{{ index }}: {{ item }}</p>
</each>
```

#### Objects <a href="#objects" id="objects"></a>

```html
<each loop="value, key in page.myObject">
  <p>{{ key }}: {{ value }}</p>
</each>
```

#### Loop props <a href="#loop-props" id="loop-props"></a>

While inside a loop, you will have access to a special object called `{{ loop }}`. This object provides you with valuable information about the current loop iteration.

* `loop.index` - the current iteration of the loop (0 indexed)
* `loop.remaining` - number of iterations until the end (0 indexed)
* `loop.first` - boolean indicating if it's the first iteration
* `loop.last` - boolean indicating if it's the last iteration
* `loop.length` - total number of items

### Switches <a href="#switchs" id="switchs"></a>

```html
<switch expression="page.user.role">
  <case n="'admin'">
    <p>Welcome, admin! You have full access to all features.</p>
  </case>
  <case n="'editor'">
    <p>Welcome, editor! You can create and edit content on the platform.</p>
  </case>
  <case n="'subscriber'">
    <p>Welcome, subscriber! Enjoy exclusive content and updates.</p>
  </case>
  <default>
    <p>Welcome, guest! Sign up or log in to access more features.</p>
  </default>
</switch>
```

### Fetch <a href="#fetch" id="fetch"></a>

```html
<fetch url="https://jsonplaceholder.typicode.com/users">
  <each loop="user in res">
    {{ user.name }}
  </each>
</fetch>
```

### Raw <a href="#raw" id="raw"></a>

If you want to ignore expressions in a block of code

```html
<raw>
  This will not be parsed:
  <if condition="page.env">
    {{ page.env }}
  </if>
  Neither will this expression: {{ page.env }}
</raw>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://documentation.mailhub.sh/editor/dynamic-expressions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
