github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/render/html_test.go (about) 1 package render 2 3 import ( 4 "bytes" 5 "strings" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 ) 10 11 const htmlLayout = "layout.html" 12 const htmlAltLayout = "alt_layout.plush.html" 13 const htmlTemplate = "my-template.html" 14 15 func Test_HTML_WithoutLayout(t *testing.T) { 16 r := require.New(t) 17 18 e := NewEngine() 19 20 box := e.TemplatesBox 21 r.NoError(box.AddString(htmlTemplate, "<%= name %>")) 22 23 h := e.HTML(htmlTemplate) 24 r.Equal("text/html; charset=utf-8", h.ContentType()) 25 bb := &bytes.Buffer{} 26 27 r.NoError(h.Render(bb, Data{"name": "Mark"})) 28 r.Equal("Mark", strings.TrimSpace(bb.String())) 29 } 30 31 func Test_HTML_WithLayout(t *testing.T) { 32 r := require.New(t) 33 34 e := NewEngine() 35 e.HTMLLayout = htmlLayout 36 37 box := e.TemplatesBox 38 r.NoError(box.AddString(htmlTemplate, "<%= name %>")) 39 r.NoError(box.AddString(htmlLayout, "<body><%= yield %></body>")) 40 41 h := e.HTML(htmlTemplate) 42 r.Equal("text/html; charset=utf-8", h.ContentType()) 43 bb := &bytes.Buffer{} 44 45 r.NoError(h.Render(bb, Data{"name": "Mark"})) 46 r.Equal("<body>Mark</body>", strings.TrimSpace(bb.String())) 47 } 48 49 func Test_HTML_WithLayout_Override(t *testing.T) { 50 r := require.New(t) 51 52 e := NewEngine() 53 e.HTMLLayout = htmlLayout 54 55 box := e.TemplatesBox 56 r.NoError(box.AddString(htmlTemplate, "<%= name %>")) 57 r.NoError(box.AddString(htmlLayout, "<body><%= yield %></body>")) 58 r.NoError(box.AddString(htmlAltLayout, "<html><%= yield %></html>")) 59 60 h := e.HTML(htmlTemplate, htmlAltLayout) 61 r.Equal("text/html; charset=utf-8", h.ContentType()) 62 bb := &bytes.Buffer{} 63 64 r.NoError(h.Render(bb, Data{"name": "Mark"})) 65 r.Equal("<html>Mark</html>", strings.TrimSpace(bb.String())) 66 }