From 57e35f3953a9f9d19931787e58f37f07a7b03b75 Mon Sep 17 00:00:00 2001 From: William Poussier Date: Wed, 27 Sep 2017 17:37:20 +0200 Subject: [PATCH] Allow multiple tables to be used in templates. --- default.go | 131 ++++++++++++++++++++++++-------------------- examples/main.go | 3 +- examples/receipt.go | 40 +++++++------- flat.go | 131 ++++++++++++++++++++++++-------------------- hermes.go | 6 +- hermes_test.go | 65 +++++++++++----------- 6 files changed, 205 insertions(+), 171 deletions(-) diff --git a/default.go b/default.go index 2ec8d4f..2f5cbaa 100644 --- a/default.go +++ b/default.go @@ -207,8 +207,11 @@ func (dt *Default) HTMLTemplate() string { /* Data table ------------------------------ */ .data-wrapper { width: 100%; - margin: 0; - padding: 35px 0; + margin: 25px 0 25px 15px; + padding-top: 15px; + } + .data-wrapper caption { + text-align: left; } .data-table { width: 100%; @@ -307,54 +310,59 @@ func (dt *Default) HTMLTemplate() string { {{ end }} {{ end }} - - {{ with .Email.Body.Table }} - {{ $data := .Data }} - {{ $columns := .Columns }} - {{ if gt (len $data) 0 }} - - - + {{ range $cell := $row }} + + {{ end }} + + {{ end }} +
- - - {{ $col := index $data 0 }} - {{ range $entry := $col }} - - {{ end }} - - {{ range $row := $data }} + + {{ with .Email.Body.Tables }} + {{ range $table := . }} + {{ $data := .Data }} + {{ $columns := .Columns }} + {{ if gt (len $data) 0 }} +
-

{{ $entry.Key }}

-
+ {{ if $table.Title }} + + {{ end }} + + - -
{{ $table.Title }}
+ - {{ range $cell := $row }} - +

{{ $entry.Key }}

+ {{ end }} - {{ end }} -
- {{ $cell.Value }} -
-
+ {{ range $row := $data }} +
+ {{ $cell.Value }} +
+ + + + {{ end }} {{ end }} {{ end }} @@ -454,27 +462,32 @@ func (dt *Default) PlainTextTemplate() string { {{ end }} {{ end }} - {{ with .Email.Body.Table }} - {{ $data := .Data }} - {{ $columns := .Columns }} - {{ if gt (len $data) 0 }} - - - {{ $col := index $data 0 }} - {{ range $entry := $col }} - - {{ end }} - - {{ range $row := $data }} + {{ with .Email.Body.Tables }} + {{ range $table := . }} + {{ $data := .Data }} + {{ $columns := .Columns }} + {{ if gt (len $data) 0 }} + {{ if $table.Title }} + {{ $table.Title }} + {{ end }} +
{{ $entry.Key }}
- {{ range $cell := $row }} - + {{ $col := index $data 0 }} + {{ range $entry := $col }} + {{ end }} - {{ end }} -
- {{ $cell.Value }} - {{ $entry.Key }}
+ {{ range $row := $data }} + + {{ range $cell := $row }} + + {{ $cell.Value }} + + {{ end }} + + {{ end }} + + {{ end }} {{ end }} {{ end }} {{ with .Email.Body.Actions }} diff --git a/examples/main.go b/examples/main.go index 00fc9ac..b680b87 100644 --- a/examples/main.go +++ b/examples/main.go @@ -2,9 +2,10 @@ package main import ( "fmt" - "github.com/matcornic/hermes" "io/ioutil" "os" + + "github.com/matcornic/hermes" ) type example interface { diff --git a/examples/receipt.go b/examples/receipt.go index c42529c..55c73aa 100644 --- a/examples/receipt.go +++ b/examples/receipt.go @@ -18,26 +18,28 @@ func (r *receipt) Email() hermes.Email { Intros: []string{ "Your order has been processed successfully.", }, - Table: hermes.Table{ - Data: [][]hermes.Entry{ - { - {Key: "Item", Value: "Golang"}, - {Key: "Description", Value: "Open source programming language that makes it easy to build simple, reliable, and efficient software"}, - {Key: "Price", Value: "$10.99"}, - }, - { - {Key: "Item", Value: "Hermes"}, - {Key: "Description", Value: "Programmatically create beautiful e-mails using Golang."}, - {Key: "Price", Value: "$1.99"}, - }, - }, - Columns: hermes.Columns{ - CustomWidth: map[string]string{ - "Item": "20%", - "Price": "15%", + Tables: []hermes.Table{ + { + Data: [][]hermes.Entry{ + { + {Key: "Item", Value: "Golang"}, + {Key: "Description", Value: "Open source programming language that makes it easy to build simple, reliable, and efficient software"}, + {Key: "Price", Value: "$10.99"}, + }, + { + {Key: "Item", Value: "Hermes"}, + {Key: "Description", Value: "Programmatically create beautiful e-mails using Golang."}, + {Key: "Price", Value: "$1.99"}, + }, }, - CustomAlignement: map[string]string{ - "Price": "right", + Columns: hermes.Columns{ + CustomWidth: map[string]string{ + "Item": "20%", + "Price": "15%", + }, + CustomAlignement: map[string]string{ + "Price": "right", + }, }, }, }, diff --git a/flat.go b/flat.go index 26a4f32..ce1c3f4 100644 --- a/flat.go +++ b/flat.go @@ -213,8 +213,11 @@ func (dt *Flat) HTMLTemplate() string { /* Data table ------------------------------ */ .data-wrapper { width: 100%; - margin: 0; - padding: 35px 0; + margin: 25px 0 25px 15px; + padding-top: 15px; + } + .data-wrapper caption { + text-align: left; } .data-table { width: 100%; @@ -307,54 +310,59 @@ func (dt *Flat) HTMLTemplate() string { {{ end }} {{ end }} - - {{ with .Email.Body.Table }} - {{ $data := .Data }} - {{ $columns := .Columns }} - {{ if gt (len $data) 0 }} - - - + {{ range $cell := $row }} + + {{ end }} + + {{ end }} +
- - - {{ $col := index $data 0 }} - {{ range $entry := $col }} - - {{ end }} - - {{ range $row := $data }} + + {{ with .Email.Body.Tables }} + {{ range $table := . }} + {{ $data := .Data }} + {{ $columns := .Columns }} + {{ if gt (len $data) 0 }} +
-

{{ $entry.Key }}

-
+ {{ if $table.Title }} + + {{ end }} + + - -
{{ $table.Title }}
+ - {{ range $cell := $row }} - +

{{ $entry.Key }}

+ {{ end }} - {{ end }} -
- {{ $cell.Value }} -
-
+ {{ range $row := $data }} +
+ {{ $cell.Value }} +
+ + + + {{ end }} {{ end }} {{ end }} @@ -454,27 +462,32 @@ func (dt *Flat) PlainTextTemplate() string { {{ end }} {{ end }} - {{ with .Email.Body.Table }} - {{ $data := .Data }} - {{ $columns := .Columns }} - {{ if gt (len $data) 0 }} - - - {{ $col := index $data 0 }} - {{ range $entry := $col }} - - {{ end }} - - {{ range $row := $data }} + {{ with .Email.Body.Tables }} + {{ range $table := . }} + {{ $data := .Data }} + {{ $columns := .Columns }} + {{ if gt (len $data) 0 }} + {{ if $table.Title }} + {{ $table.Title }} + {{ end }} +
{{ $entry.Key }}
- {{ range $cell := $row }} - + {{ $col := index $data 0 }} + {{ range $entry := $col }} + {{ end }} - {{ end }} -
- {{ $cell.Value }} - {{ $entry.Key }}
+ {{ range $row := $data }} + + {{ range $cell := $row }} + + {{ $cell.Value }} + + {{ end }} + + {{ end }} + + {{ end }} {{ end }} {{ end }} {{ with .Email.Body.Actions }} diff --git a/hermes.go b/hermes.go index a76a5d1..7bc79a0 100644 --- a/hermes.go +++ b/hermes.go @@ -2,11 +2,12 @@ package hermes import ( "bytes" + "html/template" + "github.com/Masterminds/sprig" "github.com/imdario/mergo" "github.com/jaytaylor/html2text" "github.com/russross/blackfriday" - "html/template" ) // Hermes is an instance of the hermes email generator @@ -62,7 +63,7 @@ type Body struct { Name string // The name of the contacted person Intros []string // Intro sentences, first displayed in the email Dictionary []Entry // A list of key+value (useful for displaying parameters/settings/personal info) - Table Table // Table is an table where you can put data (pricing grid, a bill, and so on) + Tables []Table // Tables is a list of tables where you can put data (pricing grid, a bill, and so on) Actions []Action // Actions are a list of actions that the user will be able to execute via a button click Outros []string // Outro sentences, last displayed in the email Greeting string // Greeting for the contacted person (default to 'Hi') @@ -86,6 +87,7 @@ type Entry struct { // Table is an table where you can put data (pricing grid, a bill, and so on) type Table struct { + Title string // Table title Data [][]Entry // Contains data Columns Columns // Contains meta-data for display purpose (width, alignement) } diff --git a/hermes_test.go b/hermes_test.go index fccdb64..bf71ba2 100644 --- a/hermes_test.go +++ b/hermes_test.go @@ -1,8 +1,9 @@ package hermes import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) var testedThemes = []Theme{ @@ -55,26 +56,28 @@ func (ed *SimpleExample) getExample() (Hermes, Email) { {"Lastname", "Snow"}, {"Birthday", "01/01/283"}, }, - Table: Table{ - Data: [][]Entry{ - { - {Key: "Item", Value: "Golang"}, - {Key: "Description", Value: "Open source programming language that makes it easy to build simple, reliable, and efficient software"}, - {Key: "Price", Value: "$10.99"}, - }, - { - {Key: "Item", Value: "Hermes"}, - {Key: "Description", Value: "Programmatically create beautiful e-mails using Golang."}, - {Key: "Price", Value: "$1.99"}, - }, - }, - Columns: Columns{ - CustomWidth: map[string]string{ - "Item": "20%", - "Price": "15%", + Tables: []Table{ + { + Data: [][]Entry{ + { + {Key: "Item", Value: "Golang"}, + {Key: "Description", Value: "Open source programming language that makes it easy to build simple, reliable, and efficient software"}, + {Key: "Price", Value: "$10.99"}, + }, + { + {Key: "Item", Value: "Hermes"}, + {Key: "Description", Value: "Programmatically create beautiful e-mails using Golang."}, + {Key: "Price", Value: "$1.99"}, + }, }, - CustomAlignement: map[string]string{ - "Price": "right", + Columns: Columns{ + CustomWidth: map[string]string{ + "Item": "20%", + "Price": "15%", + }, + CustomAlignement: map[string]string{ + "Price": "right", + }, }, }, }, @@ -263,7 +266,7 @@ func (ed *WithFreeMarkdownContent) getExample() (Hermes, Email) { Body{ Name: "Jon Snow", FreeMarkdown: ` -> _Hermes_ service will shutdown the **1st August 2017** for maintenance operations. +> _Hermes_ service will shutdown the **1st August 2017** for maintenance operations. Services will be unavailable based on the following schedule: @@ -284,13 +287,15 @@ Feel free to contact us for any question regarding this matter at [support@herme Dictionary: []Entry{ {"Dictionary that should not be displayed", "Because of FreeMarkdown"}, }, - Table: Table{ - Data: [][]Entry{ - { - {Key: "Item", Value: "Golang"}, - }, - { - {Key: "Item", Value: "Hermes"}, + Tables: []Table{ + { + Data: [][]Entry{ + { + {Key: "Item", Value: "Golang"}, + }, + { + {Key: "Item", Value: "Hermes"}, + }, }, }, }, @@ -455,9 +460,7 @@ func TestHermes_Default(t *testing.T) { assert.Empty(t, email.Body.Dictionary) assert.Empty(t, email.Body.Intros) assert.Empty(t, email.Body.Outros) - assert.Empty(t, email.Body.Table.Data) - assert.Empty(t, email.Body.Table.Columns.CustomWidth) - assert.Empty(t, email.Body.Table.Columns.CustomAlignement) + assert.Len(t, email.Body.Tables, 0) assert.Empty(t, string(email.Body.FreeMarkdown)) assert.Equal(t, email.Body.Greeting, "Hi")