mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-23 22:27:38 -05:00
Merge pull request #295 from guilhermebr/master
Add option to change delims in templates
This commit is contained in:
commit
ef617f9ce4
6 changed files with 94 additions and 11 deletions
|
@ -32,18 +32,48 @@ func templatesParse(c *Controller) ([]templates.Rule, error) {
|
||||||
for c.Next() {
|
for c.Next() {
|
||||||
var rule templates.Rule
|
var rule templates.Rule
|
||||||
|
|
||||||
if c.NextArg() {
|
rule.Path = defaultTemplatePath
|
||||||
|
rule.Extensions = defaultTemplateExtensions
|
||||||
|
|
||||||
|
args := c.RemainingArgs()
|
||||||
|
|
||||||
|
switch len(args) {
|
||||||
|
case 0:
|
||||||
|
// Optional block
|
||||||
|
for c.NextBlock() {
|
||||||
|
switch c.Val() {
|
||||||
|
case "path":
|
||||||
|
args := c.RemainingArgs()
|
||||||
|
if len(args) != 1 {
|
||||||
|
return nil, c.ArgErr()
|
||||||
|
}
|
||||||
|
rule.Path = args[0]
|
||||||
|
|
||||||
|
case "ext":
|
||||||
|
args := c.RemainingArgs()
|
||||||
|
if len(args) == 0 {
|
||||||
|
return nil, c.ArgErr()
|
||||||
|
}
|
||||||
|
rule.Extensions = args
|
||||||
|
|
||||||
|
case "between":
|
||||||
|
args := c.RemainingArgs()
|
||||||
|
if len(args) != 2 {
|
||||||
|
return nil, c.ArgErr()
|
||||||
|
}
|
||||||
|
rule.Delims[0] = args[0]
|
||||||
|
rule.Delims[1] = args[1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
// First argument would be the path
|
// First argument would be the path
|
||||||
rule.Path = c.Val()
|
rule.Path = args[0]
|
||||||
|
|
||||||
// Any remaining arguments are extensions
|
// Any remaining arguments are extensions
|
||||||
rule.Extensions = c.RemainingArgs()
|
rule.Extensions = args[1:]
|
||||||
if len(rule.Extensions) == 0 {
|
if len(rule.Extensions) == 0 {
|
||||||
rule.Extensions = defaultTemplateExtensions
|
rule.Extensions = defaultTemplateExtensions
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
rule.Path = defaultTemplatePath
|
|
||||||
rule.Extensions = defaultTemplateExtensions
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, ext := range rule.Extensions {
|
for _, ext := range rule.Extensions {
|
||||||
|
@ -52,7 +82,6 @@ func templatesParse(c *Controller) ([]templates.Rule, error) {
|
||||||
|
|
||||||
rules = append(rules, rule)
|
rules = append(rules, rule)
|
||||||
}
|
}
|
||||||
|
|
||||||
return rules, nil
|
return rules, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,9 @@ package setup
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/mholt/caddy/middleware/templates"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mholt/caddy/middleware/templates"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTemplates(t *testing.T) {
|
func TestTemplates(t *testing.T) {
|
||||||
|
@ -40,7 +41,11 @@ func TestTemplates(t *testing.T) {
|
||||||
if fmt.Sprint(myHandler.Rules[0].IndexFiles) != fmt.Sprint(indexFiles) {
|
if fmt.Sprint(myHandler.Rules[0].IndexFiles) != fmt.Sprint(indexFiles) {
|
||||||
t.Errorf("Expected %v to be the Default Index files", indexFiles)
|
t.Errorf("Expected %v to be the Default Index files", indexFiles)
|
||||||
}
|
}
|
||||||
|
if myHandler.Rules[0].Delims != [2]string{} {
|
||||||
|
t.Errorf("Expected %v to be the Default Delims", [2]string{})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTemplatesParse(t *testing.T) {
|
func TestTemplatesParse(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
inputTemplateConfig string
|
inputTemplateConfig string
|
||||||
|
@ -50,19 +55,32 @@ func TestTemplatesParse(t *testing.T) {
|
||||||
{`templates /api1`, false, []templates.Rule{{
|
{`templates /api1`, false, []templates.Rule{{
|
||||||
Path: "/api1",
|
Path: "/api1",
|
||||||
Extensions: defaultTemplateExtensions,
|
Extensions: defaultTemplateExtensions,
|
||||||
|
Delims: [2]string{},
|
||||||
}}},
|
}}},
|
||||||
{`templates /api2 .txt .htm`, false, []templates.Rule{{
|
{`templates /api2 .txt .htm`, false, []templates.Rule{{
|
||||||
Path: "/api2",
|
Path: "/api2",
|
||||||
Extensions: []string{".txt", ".htm"},
|
Extensions: []string{".txt", ".htm"},
|
||||||
|
Delims: [2]string{},
|
||||||
}}},
|
}}},
|
||||||
|
|
||||||
{`templates /api3 .htm .html
|
{`templates /api3 .htm .html
|
||||||
templates /api4 .txt .tpl `, false, []templates.Rule{{
|
templates /api4 .txt .tpl `, false, []templates.Rule{{
|
||||||
Path: "/api3",
|
Path: "/api3",
|
||||||
Extensions: []string{".htm", ".html"},
|
Extensions: []string{".htm", ".html"},
|
||||||
|
Delims: [2]string{},
|
||||||
}, {
|
}, {
|
||||||
Path: "/api4",
|
Path: "/api4",
|
||||||
Extensions: []string{".txt", ".tpl"},
|
Extensions: []string{".txt", ".tpl"},
|
||||||
|
Delims: [2]string{},
|
||||||
|
}}},
|
||||||
|
{`templates {
|
||||||
|
path /api5
|
||||||
|
ext .html
|
||||||
|
between {% %}
|
||||||
|
}`, false, []templates.Rule{{
|
||||||
|
Path: "/api5",
|
||||||
|
Extensions: []string{".html"},
|
||||||
|
Delims: [2]string{"{%", "%}"},
|
||||||
}}},
|
}}},
|
||||||
}
|
}
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
|
|
|
@ -33,9 +33,18 @@ func (t Templates) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
|
||||||
// Create execution context
|
// Create execution context
|
||||||
ctx := middleware.Context{Root: t.FileSys, Req: r, URL: r.URL}
|
ctx := middleware.Context{Root: t.FileSys, Req: r, URL: r.URL}
|
||||||
|
|
||||||
|
// New template
|
||||||
|
templateName := filepath.Base(fpath)
|
||||||
|
tpl := template.New(templateName)
|
||||||
|
|
||||||
|
// Set delims
|
||||||
|
if rule.Delims != [2]string{} {
|
||||||
|
tpl.Delims(rule.Delims[0], rule.Delims[1])
|
||||||
|
}
|
||||||
|
|
||||||
// Build the template
|
// Build the template
|
||||||
templatePath := filepath.Join(t.Root, fpath)
|
templatePath := filepath.Join(t.Root, fpath)
|
||||||
tpl, err := template.ParseFiles(templatePath)
|
tpl, err := tpl.ParseFiles(templatePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
return http.StatusNotFound, nil
|
return http.StatusNotFound, nil
|
||||||
|
@ -82,4 +91,5 @@ type Rule struct {
|
||||||
Path string
|
Path string
|
||||||
Extensions []string
|
Extensions []string
|
||||||
IndexFiles []string
|
IndexFiles []string
|
||||||
|
Delims [2]string
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@ func Test(t *testing.T) {
|
||||||
Extensions: []string{".html", ".htm"},
|
Extensions: []string{".html", ".htm"},
|
||||||
IndexFiles: []string{"index.html", "index.htm"},
|
IndexFiles: []string{"index.html", "index.htm"},
|
||||||
Path: "/images",
|
Path: "/images",
|
||||||
|
Delims: [2]string{"{%", "%}"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Root: "./testdata",
|
Root: "./testdata",
|
||||||
|
@ -94,6 +95,30 @@ func Test(t *testing.T) {
|
||||||
t.Fatalf("Test: the expected body %v is different from the response one: %v", expectedBody, respBody)
|
t.Fatalf("Test: the expected body %v is different from the response one: %v", expectedBody, respBody)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Test tmpl on /images/img2.htm
|
||||||
|
*/
|
||||||
|
req, err = http.NewRequest("GET", "/images/img2.htm", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Could not create HTTP request: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rec = httptest.NewRecorder()
|
||||||
|
|
||||||
|
tmpl.ServeHTTP(rec, req)
|
||||||
|
|
||||||
|
if rec.Code != http.StatusOK {
|
||||||
|
t.Fatalf("Test: Wrong response code: %d, should be %d", rec.Code, http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
respBody = rec.Body.String()
|
||||||
|
expectedBody = `<!DOCTYPE html><html><head><title>img</title></head><body>{{.Include "header.html"}}</body></html>
|
||||||
|
`
|
||||||
|
|
||||||
|
if respBody != expectedBody {
|
||||||
|
t.Fatalf("Test: the expected body %v is different from the response one: %v", expectedBody, respBody)
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Test tmplroot on /root.html
|
* Test tmplroot on /root.html
|
||||||
*/
|
*/
|
||||||
|
|
2
middleware/templates/testdata/images/img.htm
vendored
2
middleware/templates/testdata/images/img.htm
vendored
|
@ -1 +1 @@
|
||||||
<!DOCTYPE html><html><head><title>img</title></head><body>{{.Include "header.html"}}</body></html>
|
<!DOCTYPE html><html><head><title>img</title></head><body>{%.Include "header.html"%}</body></html>
|
||||||
|
|
1
middleware/templates/testdata/images/img2.htm
vendored
Normal file
1
middleware/templates/testdata/images/img2.htm
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
<!DOCTYPE html><html><head><title>img</title></head><body>{{.Include "header.html"}}</body></html>
|
Loading…
Reference in a new issue