Functions

Functions allow you to run groups of code within the template renderer.

Assignment

Functions are assigned the same way that variables are assigned, as they are passed to the renderer data object.

Note: If a function returns anything, it should return a string otherwise the result will be converted to a string which means an object will display as [object Object]

const crypto = require('crypto')

Router.get('/', client => client.response.render('main.mix', {
  md5: (string) => crypto.createHash('md5').update(string).digest('hex'),
  emails: [ 'one@example.com', 'two@example.com', 'three@example.com' ]
}))

Template access

A function is used in a similar way that a variable is used. So, it must be between double braces {{}} and it is formatted the same as a function in javascript. The function can also take parameters just like a javascript function.

<html>
  <body>
    <each :="email in {{$emails}}">
      <img src="https://www.gravatar.com/avatar/{{md5($email)}}">
    </each>
  </body>
</html>