🐕
Corgi
  • 🐕About Corgi
  • 🎓Learning Corgi
    • 📂File Structure
    • 🎉Getting Started
    • 🔟Code
    • 📄Arrow Blocks
    • 🪄If and Switch
    • ✏️Interpolation
    • 🔁For
    • 🤝Attributes
    • 🎭Expressions
    • 👮Security and Escaping
    • 💉Nonce Injection
    • ➡️Block Expansions
    • ➕Mixins
    • 📚Libraries
    • ✨The Standard Library
    • 💬Comments
    • ⛓️Filters
    • 🖨️Include
    • 👪Inheritance (Extending)
    • ⚡Breaking Changes
Powered by GitBook
On this page
  • Expression Interpolation
  • Text Interpolation
  • Interpolated Elements
  • String Interpolation

Was this helpful?

  1. Learning Corgi

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.
var mypackage.Constant = "🎉"

Interpolation("variables")
<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>

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.#[    ]
  
<p>
  There are four lights&mdash;
  just like there are four trailing spaces.    
</p>

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]!
<p>Suddenly, I feel so <strong>strong</strong>!</p>

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}!

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

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!]
<p>
  Attributes <span style="font-size: 120%">work</span>.
  So do classes: <strong class="yellow">Look, I'm yellow!</strong>
</p>

String Interpolation

Corgi also allows expression interpolation in strings:

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

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

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

PreviousIf and SwitchNextFor

Last updated 1 year ago

Was this helpful?

🎓
✏️