Helpers

This document only applies when using https://github.com/gobuffalo/buffalo/render.
Please see github.com/gobuffalo/velvet for more details on the underlying templating package.


    Each Statements (Array)

    When looping through arrays or slices, the block being looped through will have access to the "global" context, as well as have four new variables available within that block:

    • @first [bool] - is this the first pass through the iteration?
    • @last [bool] - is this the last pass through the iteration?
    • @index [int] - the counter of where in the loop you are, starting with 0.
    • @value - the current element in the array or slice that is being iterated over.
    <ul>
      {{#each names}}
        <li>{{ @index }} - {{ @value }}</li>
      {{/each}}
    </ul>
    

    By using "block parameters" you can change the "key" of the element being accessed from @value to a key of your choosing.

    <ul>
      {{#each names as |name|}}
        <li>{{ name }}</li>
      {{/each}}
    </ul>
    

    To change both the key and the index name you can pass two "block parameters"; the first being the new name for the index and the second being the name for the element.

    <ul>
      {{#each names as |index, name|}}
        <li>{{ index }} - {{ name }}</li>
      {{/each}}
    </ul>
    

    Each Statements (Maps)

    Looping through maps using the each helper is also supported, and follows very similar guidelines to looping through arrays.

    • @first [bool] - is this the first pass through the iteration?
    • @last [bool] - is this the last pass through the iteration?
    • @key - the key of the pair being accessed.
    • @value - the value of the pair being accessed.
    <ul>
      {{#each users}}
        <li>{{ @key }} - {{ @value }}</li>
      {{/each}}
    </ul>
    

    By using "block parameters" you can change the "key" of the element being accessed from @value to a key of your choosing.

    <ul>
      {{#each users as |user|}}
        <li>{{ @key }} - {{ user }}</li>
      {{/each}}
    </ul>
    

    To change both the key and the value name you can pass two "block parameters"; the first being the new name for the key and the second being the name for the value.

    <ul>
      {{#each users as |key, user|}}
        <li>{{ key }} - {{ user }}</li>
      {{/each}}
    </ul>
    

    Other Builtin Helpers

    • json - returns a JSON marshaled string of the value passed to it.
    • js_escape - safely escapes a string to be used in a JavaScript bit of code.
    • html_escape - safely escapes a string to be used in an HTML bit of code.
    • upcase - upper cases the entire string passed to it.
    • downcase - lower cases the entire string passed to it.
    • markdown - converts markdown to HTML.
    • eq - works like the if helper, but compares to values
    • neq - the opposite of eq (not equal)
    • len - returns the length of a value

    Velvet also imports all of the helpers found https://github.com/markbates/inflect/blob/master/helpers.go