ndelitski / rancher-alarms

Will kick your ass if found unhealthy service in Rancher environment
85 stars 20 forks source link

Cannot use a template variable more than once in template #16

Closed flaccid closed 8 years ago

flaccid commented 8 years ago

I assume this is due to the code in render-template.es6:

import _ from 'lodash';
import assert from 'assert';

export default function renderTemplate(template, data) {
  var result = template;

  // interpolate string
  for (let [k,v] of _.pairs(data)) {
    result = result.replace(`#{${k}}`, v)
  }

  // check if not all variables are filled
  let freeVariables = result.match(/#\{(\S+)\}/g);
  assert(!freeVariables, `template has unresolved variables:\ntemplate: ${template}\nmissing variables: ${(freeVariables || []).join(', ')}`);

  return result;
}

For example, I use this in my email template:

                  <tr>
                    <th>Service URL</th><td><a href="#{serviceUrl}">#{serviceUrl}</a></td>
                  </tr>

This will trip returning: missing variables: #{serviceUrl}.

Possible to fix (or do we consider using a template rendering package)?

ndelitski commented 8 years ago

@flaccid yes this is exactly a bug in a template function, will fix it soon! missed global regex flag

flaccid commented 8 years ago

Tested and confirmed also, thanks.