# Interpolation

## Expression Interpolation

Corgi has a special syntax that allows you to interpolate expressions inside your text. Simply write `#{myExpression}` inside any text to evaluate the expression and include it in the text.

{% tabs %}
{% tab title="Corgi" %}

```pug
import "mypackage"

- func sayWork() string {
    return "work"
  }

func Interpolation(myArg string)

p
  > I can easily interpolate #{myArg} or
    even a global constant #{mypackage.Constant}.
p
  > I can even interpolate literals
    like #{123} or #{"a" == "b"}.
p And functions #{sayWork()} too.
```

{% endtab %}

{% tab title="HTML" %}

```go
var mypackage.Constant = "🎉"

Interpolation("variables")
```

```markup
<p>
  I can easily interpolate variables or
  even a global constant 🎉.
</p>
<p>
  I can even interpolate literals
  like 123 or false.
</p>
<p>And functions work too.</p>
```

{% endtab %}
{% endtabs %}

All expressions are escaped before getting printed.

{% hint style="info" %}
If you ever just want to type a raw `#` inside your text or strings, write `##`. The `#` is the only character in corgi that needs escaping.

```pug
p Look, a ##!
```

becomes

```markup
<p>Look, a #!</p>
```

{% endhint %}

## Text Interpolation

You can also interpolate text.

This might sound useless at first, but it allows you to add trailing spaces to lines.

If you add a `!` after the `#`, the text won't be escaped. This is super useful to use HTML character escapes.

{% tabs %}
{% tab title="Corgi" %}

```pug
p
  > There are four lights#![&mdash;]
    just like there are four trailing spaces.#[    ]
  
```

{% endtab %}

{% tab title="HTML" %}

```markup
<p>
  There are four lights&mdash;
  just like there are four trailing spaces.    
</p>
```

{% endtab %}
{% endtabs %}

## Interpolated Elements

Sometimes you want to use a fairly short element directly in text. For that, you can use interpolated elements:

{% tabs %}
{% tab title="Corgi" %}

```pug
p Suddenly, I feel so #strong[strong]!
```

{% endtab %}

{% tab title="HTML" %}

```markup
<p>Suddenly, I feel so <strong>strong</strong>!</p>
```

{% endtab %}
{% endtabs %}

To use an expression in an interpolated element, simply swap out the `[`brackets`]` for `{`curly braces`}`. You can write everything that you can write when interpolating expressions.

{% tabs %}
{% tab title="Corgi" %}

```pug
- s := "strong"
p Suddenly, I feel so #strong{s}!
```

{% endtab %}

{% tab title="HTML" %}

```markup

<p>Suddenly, I feel so <strong>strong</strong>!</p>
```

{% endtab %}
{% endtabs %}

You can also add classes and attributes to the interpolated element. Don't overdo it though, long chains of classes and attributes hurt readability.

{% tabs %}
{% tab title="Corgi" %}

```pug
p
  > Attributes #span(style="font-size: 120%")[work].
    So do classes: #strong.yellow[Look, I'm yellow!]

```

{% endtab %}

{% tab title="HTML" %}

```markup
<p>
  Attributes <span style="font-size: 120%">work</span>.
  So do classes: <strong class="yellow">Look, I'm yellow!</strong>
</p>
```

{% endtab %}
{% endtabs %}

## String Interpolation

Corgi also allows expression interpolation in strings:

{% tabs %}
{% tab title="Corgi" %}

<pre class="language-pug"><code class="lang-pug"><strong>- path := "foo/bar"
</strong><strong>a(href="/base/path/#{path}") Click me
</strong></code></pre>

{% endtab %}

{% tab title="HTML" %}

```markup

<a href="/base/path/foo/bar">Click me</a>
```

{% endtab %}
{% endtabs %}

{% hint style="warning" %}
This also means that you need to escape hashes in strings using the regular double hash escape.
{% endhint %}


---

# 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://mavolin.gitbook.io/corgi/learning-corgi/interpolation.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.
