diff --git a/Sources/Leaf/Application+Leaf.swift b/Sources/Leaf/Application+Leaf.swift index c2f4e05..b118db8 100644 --- a/Sources/Leaf/Application+Leaf.swift +++ b/Sources/Leaf/Application+Leaf.swift @@ -19,14 +19,15 @@ extension Application { public let application: Application public var renderer: LeafRenderer { - .init( + var userInfo = self.userInfo + userInfo["application"] = self + + return .init( configuration: self.configuration, cache: self.cache, files: self.files, eventLoop: self.application.eventLoopGroup.next(), - userInfo: [ - "application": self - ] + userInfo: userInfo ) } @@ -67,6 +68,15 @@ extension Application { self.storage.cache = newValue } } + + public var userInfo: [AnyHashable: Any] { + get { + self.storage.userInfo + } + nonmutating set { + self.storage.userInfo = newValue + } + } var storage: Storage { if let existing = self.application.storage[Key.self] { @@ -87,10 +97,12 @@ extension Application { var configuration: LeafConfiguration? var files: LeafFiles? var tags: [String: LeafTag] + var userInfo: [AnyHashable: Any] init() { self.cache = DefaultLeafCache() self.tags = LeafKit.defaultTags + self.userInfo = [:] } } } diff --git a/Sources/Leaf/Request+Leaf.swift b/Sources/Leaf/Request+Leaf.swift index 48940ef..3ceb891 100644 --- a/Sources/Leaf/Request+Leaf.swift +++ b/Sources/Leaf/Request+Leaf.swift @@ -2,16 +2,17 @@ import Vapor extension Request { var leaf: LeafRenderer { - .init( + var userInfo = self.application.leaf.userInfo + userInfo["request"] = self + userInfo["application"] = self.application + + return .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 - ] + userInfo: userInfo ) } } diff --git a/Tests/LeafTests/LeafTests.swift b/Tests/LeafTests/LeafTests.swift index b34245e..f582d9e 100644 --- a/Tests/LeafTests/LeafTests.swift +++ b/Tests/LeafTests/LeafTests.swift @@ -54,6 +54,44 @@ class LeafTests: XCTestCase { XCTAssertEqual(res.body.string, "Hello vapor @ /test-file") } } + + func testContextUserInfo() throws { + var test = TestFiles() + test.files["/foo.leaf"] = """ + Hello #custom()! + """ + + struct CustomTag: LeafTag { + + func render(_ ctx: LeafContext) throws -> LeafData { + let info = ctx.userInfo["info"] as? String ?? "" + + return .string(info) + } + } + + let app = Application(.testing) + defer { app.shutdown() } + + app.views.use(.leaf) + app.leaf.configuration.rootDirectory = "/" + app.leaf.cache.isEnabled = false + app.leaf.tags["custom"] = CustomTag() + app.leaf.files = test + app.leaf.userInfo["info"] = "World" + + app.get("test-file") { req in + req.view.render("foo", [ + "name": "vapor" + ]) + } + + try app.test(.GET, "test-file") { res in + XCTAssertEqual(res.status, .ok) + XCTAssertEqual(res.headers.contentType, .html) + XCTAssertEqual(res.body.string, "Hello World!") + } + } } struct TestFiles: LeafFiles {