Browse Source

Added early catching of invalid template values. Fixes #193

pull/233/head
Jordan Wright 5 years ago
parent
commit
83a17e8c2d
2 changed files with 40 additions and 1 deletions
  1. +37
    -1
      models/template.go
  2. +3
    -0
      static/js/app/templates.js

+ 37
- 1
models/template.go View File

@ -1,7 +1,9 @@
package models
import (
"bytes"
"errors"
"html/template"
"time"
"github.com/jinzhu/gorm"
@ -33,7 +35,41 @@ func (t *Template) Validate() error {
case t.Text == "" && t.HTML == "":
return ErrTemplateMissingParameter
}
return nil
var buff bytes.Buffer
// Test that the variables used in the template
// validate with no issues
td := struct {
Result
URL string
TrackingURL string
Tracker string
From string
}{
Result{
Email: "foo@bar.com",
FirstName: "Foo",
LastName: "Bar",
Position: "Test",
},
"http://foo.bar",
"http://foo.bar/track",
"<img src='http://foo.bar/track",
"John Doe <foo@bar.com>",
}
tmpl, err := template.New("html_template").Parse(t.HTML)
if err != nil {
return err
}
err = tmpl.Execute(&buff, td)
if err != nil {
return err
}
tmpl, err = template.New("text_template").Parse(t.Text)
if err != nil {
return err
}
err = tmpl.Execute(&buff, td)
return err
}
// GetTemplates returns the templates owned by the given user.

+ 3
- 0
static/js/app/templates.js View File

@ -49,6 +49,9 @@ function save(idx) {
load()
dismiss()
})
.error(function(data) {
modalError(data.responseJSON.message)
})
} else {
// Submit the template
api.templates.post(template)

Loading…
Cancel
Save