✏️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.

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.

All expressions are escaped before getting printed.

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.

p Look, a ##!

becomes

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

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.

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

Interpolated Elements

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

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

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.

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

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

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

String Interpolation

Corgi also allows expression interpolation in strings:

- path := "foo/bar"
a(href="/base/path/#{path}") Click me

This also means that you need to escape hashes in strings using the regular double hash escape.

Last updated