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
Łukasz Nowak
caddy
Commits
f28a159b
Commit
f28a159b
authored
Apr 18, 2017
by
Matt Holt
Committed by
GitHub
Apr 18, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1591 from tw4452852/template_funcs
template: support custom functions
parents
236341f7
f77a7a80
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
8 additions
and
1 deletion
+8
-1
caddyhttp/httpserver/context.go
caddyhttp/httpserver/context.go
+5
-1
caddyhttp/templates/templates.go
caddyhttp/templates/templates.go
+3
-0
No files found.
caddyhttp/httpserver/context.go
View file @
f28a159b
...
...
@@ -244,6 +244,9 @@ func (c Context) Markdown(filename string) (string, error) {
return
string
(
markdown
),
nil
}
// TemplateFuncs contains user defined functions
var
TemplateFuncs
=
template
.
FuncMap
{}
// ContextInclude opens filename using fs and executes a template with the context ctx.
// This does the same thing that Context.Include() does, but with the ability to provide
// your own context so that the included files can have access to additional fields your
...
...
@@ -261,7 +264,8 @@ func ContextInclude(filename string, ctx interface{}, fs http.FileSystem) (strin
return
""
,
err
}
tpl
,
err
:=
template
.
New
(
filename
)
.
Parse
(
string
(
body
))
tpl
:=
template
.
New
(
filename
)
.
Funcs
(
TemplateFuncs
)
tpl
,
err
=
tpl
.
Parse
(
string
(
body
))
if
err
!=
nil
{
return
""
,
err
}
...
...
caddyhttp/templates/templates.go
View file @
f28a159b
...
...
@@ -44,6 +44,9 @@ func (t Templates) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
tpl
.
Delims
(
rule
.
Delims
[
0
],
rule
.
Delims
[
1
])
}
// Add custom functions
tpl
.
Funcs
(
httpserver
.
TemplateFuncs
)
// Build the template
templatePath
:=
filepath
.
Join
(
t
.
Root
,
fpath
)
tpl
,
err
:=
tpl
.
ParseFiles
(
templatePath
)
...
...
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