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
55801b48
Commit
55801b48
authored
Apr 18, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More template functions
parent
3ec870cb
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
1 deletion
+29
-1
middleware/templates/context.go
middleware/templates/context.go
+28
-0
middleware/templates/templates.go
middleware/templates/templates.go
+1
-1
No files found.
middleware/templates/context.go
View file @
55801b48
...
...
@@ -3,6 +3,7 @@ package templates
import
(
"io/ioutil"
"net/http"
"time"
)
// This file contains the context and functions available for
...
...
@@ -11,6 +12,7 @@ import (
// context is the context with which templates are executed.
type
context
struct
{
root
http
.
FileSystem
req
*
http
.
Request
}
// Include returns the contents of filename relative to the site root
...
...
@@ -22,3 +24,29 @@ func (c context) Include(filename string) (string, error) {
body
,
err
:=
ioutil
.
ReadAll
(
file
)
return
string
(
body
),
err
}
// Date returns the current timestamp in the specified format
func
(
c
context
)
Date
(
format
string
)
string
{
return
time
.
Now
()
.
Format
(
format
)
}
// Cookie gets the value of a cookie with name name.
func
(
c
context
)
Cookie
(
name
string
)
string
{
cookies
:=
c
.
req
.
Cookies
()
for
_
,
cookie
:=
range
cookies
{
if
cookie
.
Name
==
name
{
return
cookie
.
Value
}
}
return
""
}
// Header gets the value of a request header with field name.
func
(
c
context
)
Header
(
name
string
)
string
{
return
c
.
req
.
Header
.
Get
(
name
)
}
// RemoteAddr gets the address of the client making the request.
func
(
c
context
)
RemoteAddr
()
string
{
return
c
.
req
.
RemoteAddr
}
middleware/templates/templates.go
View file @
55801b48
...
...
@@ -38,7 +38,7 @@ func (t Templates) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
for
_
,
ext
:=
range
rule
.
Extensions
{
if
reqExt
==
ext
{
// Create execution context
ctx
:=
context
{
root
:
http
.
Dir
(
t
.
Root
)}
ctx
:=
context
{
root
:
http
.
Dir
(
t
.
Root
)
,
req
:
r
}
// Build the template
tpl
,
err
:=
template
.
ParseFiles
(
t
.
Root
+
r
.
URL
.
Path
)
...
...
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