-
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from vapor/tn-userinfo
Tags config, files protocol, and context passing
- Loading branch information
Showing
5 changed files
with
158 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import Vapor | ||
|
||
extension LeafRenderer: ViewRenderer { | ||
public func `for`(_ request: Request) -> ViewRenderer { | ||
request.leaf | ||
} | ||
|
||
public func render<E>(_ name: String, _ context: E) -> EventLoopFuture<View> | ||
where E: Encodable | ||
{ | ||
let data: [String: LeafData] | ||
do { | ||
data = try LeafEncoder().encode(context) | ||
} catch { | ||
return self.eventLoop.makeFailedFuture(error) | ||
} | ||
return self.render(path: name, context: data).map { buffer in | ||
return View(data: buffer) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import Vapor | ||
|
||
extension Request { | ||
var leaf: LeafRenderer { | ||
.init( | ||
configuration: self.application.leaf.configuration, | ||
tags: self.application.leaf.tags, | ||
cache: self.application.leaf.cache, | ||
files: self.application.leaf.files, | ||
eventLoop: self.eventLoop, | ||
userInfo: [ | ||
"request": self, | ||
"application": self.application | ||
] | ||
) | ||
} | ||
} | ||
|
||
extension LeafContext { | ||
public var request: Request? { | ||
self.userInfo["request"] as? Request | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters