Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
9d456bba
Commit
9d456bba
authored
Oct 29, 2015
by
Guilherme Rezende
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add argument in new optional block in templates midd to set delimiters
parent
d227bec0
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
74 additions
and
9 deletions
+74
-9
config/setup/templates.go
config/setup/templates.go
+36
-7
middleware/templates/templates.go
middleware/templates/templates.go
+11
-1
middleware/templates/templates_test.go
middleware/templates/templates_test.go
+25
-0
middleware/templates/testdata/images/img.htm
middleware/templates/testdata/images/img.htm
+1
-1
middleware/templates/testdata/images/img2.htm
middleware/templates/testdata/images/img2.htm
+1
-0
No files found.
config/setup/templates.go
View file @
9d456bba
...
...
@@ -32,18 +32,48 @@ func templatesParse(c *Controller) ([]templates.Rule, error) {
for
c
.
Next
()
{
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
rule
.
Path
=
c
.
Val
()
rule
.
Path
=
args
[
0
]
// Any remaining arguments are extensions
rule
.
Extensions
=
c
.
RemainingArgs
()
rule
.
Extensions
=
args
[
1
:
]
if
len
(
rule
.
Extensions
)
==
0
{
rule
.
Extensions
=
defaultTemplateExtensions
}
}
else
{
rule
.
Path
=
defaultTemplatePath
rule
.
Extensions
=
defaultTemplateExtensions
}
for
_
,
ext
:=
range
rule
.
Extensions
{
...
...
@@ -52,7 +82,6 @@ func templatesParse(c *Controller) ([]templates.Rule, error) {
rules
=
append
(
rules
,
rule
)
}
return
rules
,
nil
}
...
...
middleware/templates/templates.go
View file @
9d456bba
...
...
@@ -33,9 +33,18 @@ func (t Templates) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
// Create execution context
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
templatePath
:=
filepath
.
Join
(
t
.
Root
,
fpath
)
tpl
,
err
:=
t
emplate
.
ParseFiles
(
templatePath
)
tpl
,
err
:=
t
pl
.
ParseFiles
(
templatePath
)
if
err
!=
nil
{
if
os
.
IsNotExist
(
err
)
{
return
http
.
StatusNotFound
,
nil
...
...
@@ -82,4 +91,5 @@ type Rule struct {
Path
string
Extensions
[]
string
IndexFiles
[]
string
Delims
[
2
]
string
}
middleware/templates/templates_test.go
View file @
9d456bba
...
...
@@ -23,6 +23,7 @@ func Test(t *testing.T) {
Extensions
:
[]
string
{
".html"
,
".htm"
},
IndexFiles
:
[]
string
{
"index.html"
,
"index.htm"
},
Path
:
"/images"
,
Delims
:
[
2
]
string
{
"{%"
,
"%}"
},
},
},
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
)
}
/*
* 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
*/
...
...
middleware/templates/testdata/images/img.htm
View file @
9d456bba
<!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>
middleware/templates/testdata/images/img2.htm
0 → 100644
View file @
9d456bba
<!DOCTYPE html>
<html><head><title>
img
</title></head><body>
{{.Include "header.html"}}
</body></html>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment