🐕
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

Was this helpful?

  1. Learning Corgi

For

Just like with if and switch, corgi also provides a built-in for-loop.

You can write anything behind the for that you could also write behind a Go for. Just don't add a {.

Unlike Go templates, when writing a for-range loop the index goes first, just like in regular Go.

for _, elem := range []int{1, 2, 3, 4}
  p #{elem}

for _, elem := range []int{1, 2, 3, 4} {
  // p #{elem}
}
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>

Ordered Map Iteration

When iterating over maps the order is non-deterministic, meaning the elements will appear in "random" order.

If you always want to print elements in the same (sorted) order, you can use corgi's ordered range keyword, which allows you to range over maps with comparable keys (as defined by the comparable constraint) in ascending order.

- m := map[string]string{"b": "bar", "a": "foo"}
for k, v := ordered range m
  p #{k}: #{v}


  <p>a: foo</p>
  <p>b: bar</p>
PreviousInterpolationNextAttributes

Last updated 1 year ago

Was this helpful?

🎓
🔁