Documentation
Templates

Templates

Templates are an essential part of Baba Tools Notifications as they define the messages that you want to send to your users through various providers.

Depending on the provider there are various types of templates allowing you to customize the content and format of your notifications.

Templates also define what type of notification will be sent. A Email template will send an email, a SMS template will send an sms, a Github Issue template will create a Github issue, etc.

Template Engine

Baba Tools Notifications uses the Go template engine (opens in a new tab) to render the content of the notifications. Go's template engine is very flexible and powerful.

Here is an example of an email template written using Go's template engine:

<html>
    <body>
        <h1>Hello {{ .user.name }}</h1>
 
        <p>Welcome to Baba Tools.</p>
 
        <p>Here are some available services</p>
 
        <div>
            {{- range .services }}
            <div>{{ . }}</div>
            {{- end }}
        </div>
 
        {{- if .isTrial }}
        <div>Your trial expires in 30 days</div>
        {{- end }}
    </body>
</html>

Given the following json data:

{
  "user": {
    "name": "John Doe"
  },
  "services": ["Notifications", "Feedback"],
  "isTrial": true
}

Will render the following HTML:

<html>
  <body>
    <h1>Hello John Doe</h1>
 
    <p>Welcome to Baba Tools.</p>
 
    <p>Here are some available services</p>
 
    <div>
      <div>feedback</div>
      <div>notifications</div>
    </div>
    <div>Your trial expires in 30 days</div>
  </body>
</html>
💡

You can play around with Go templates in the browser using gotemplate.io (opens in a new tab).

Template Data

When you send the notification programtically whatever data you pass to the the api is available under the .data variable in the template.

For example, if you send the following data in the notification endpoint:

{
  "channelId": "<CHANNEL_ID>",
  "data": {
    "user": {
      "name": "John Doe"
    },
    "services": ["Notifications", "Feedback"],
    "isTrial": true
  }
}

You can access it in the template like this:

<html>
    <body>
        <h1>Hello {{ .data.user.name }}</h1>
 
        <p>Welcome to Baba Tools.</p>
 
        <p>Here are some available services</p>
 
        <div>
            {{- range .data.services }}
            <div>{{ . }}</div>
            {{- end }}
        </div>
 
        {{- if .data.isTrial }}
        <div>Your trial expires in 30 days</div>
        {{- end }}
    </body>
</html>

Template Types

Email

Email templates are used to send emails and work with providers that support sending emails. For example, Sendgrid and Mailgun.

Email templates are rendered using the Go html template engine (opens in a new tab). This is to ensure that the rendered HTML is safe and does not contain any malicious code.

SMS

SMS templates are used to send SMS messages and work with providers that support sending SMS messages. For example, Twilio.

SMS templates are rendered using the Go text template engine (opens in a new tab).

Slack

Slack templates are used to send messages to Slack channels and work with the Slack provider.

Slack templates are rendered using the Go text template engine (opens in a new tab).