File tree Expand file tree Collapse file tree 3 files changed +60
-9
lines changed
Expand file tree Collapse file tree 3 files changed +60
-9
lines changed Original file line number Diff line number Diff line change @@ -19,14 +19,15 @@ extension Application {
1919 public let application : Application
2020
2121 public var renderer : LeafRenderer {
22- . init(
22+ var userInfo = self . userInfo
23+ userInfo [ " application " ] = self
24+
25+ return . init(
2326 configuration: self . configuration,
2427 cache: self . cache,
2528 files: self . files,
2629 eventLoop: self . application. eventLoopGroup. next ( ) ,
27- userInfo: [
28- " application " : self
29- ]
30+ userInfo: userInfo
3031 )
3132 }
3233
@@ -67,6 +68,15 @@ extension Application {
6768 self . storage. cache = newValue
6869 }
6970 }
71+
72+ public var userInfo : [ AnyHashable : Any ] {
73+ get {
74+ self . storage. userInfo
75+ }
76+ nonmutating set {
77+ self . storage. userInfo = newValue
78+ }
79+ }
7080
7181 var storage : Storage {
7282 if let existing = self . application. storage [ Key . self] {
@@ -87,10 +97,12 @@ extension Application {
8797 var configuration : LeafConfiguration ?
8898 var files : LeafFiles ?
8999 var tags : [ String : LeafTag ]
100+ var userInfo : [ AnyHashable : Any ]
90101
91102 init ( ) {
92103 self . cache = DefaultLeafCache ( )
93104 self . tags = LeafKit . defaultTags
105+ self . userInfo = [ : ]
94106 }
95107 }
96108 }
Original file line number Diff line number Diff line change @@ -2,16 +2,17 @@ import Vapor
22
33extension Request {
44 var leaf : LeafRenderer {
5- . init(
5+ var userInfo = self . application. leaf. userInfo
6+ userInfo [ " request " ] = self
7+ userInfo [ " application " ] = self . application
8+
9+ return . init(
610 configuration: self . application. leaf. configuration,
711 tags: self . application. leaf. tags,
812 cache: self . application. leaf. cache,
913 files: self . application. leaf. files,
1014 eventLoop: self . eventLoop,
11- userInfo: [
12- " request " : self ,
13- " application " : self . application
14- ]
15+ userInfo: userInfo
1516 )
1617 }
1718}
Original file line number Diff line number Diff line change @@ -54,6 +54,44 @@ class LeafTests: XCTestCase {
5454 XCTAssertEqual ( res. body. string, " Hello vapor @ /test-file " )
5555 }
5656 }
57+
58+ func testContextUserInfo( ) throws {
59+ var test = TestFiles ( )
60+ test. files [ " /foo.leaf " ] = """
61+ Hello #custom()!
62+ """
63+
64+ struct CustomTag : LeafTag {
65+
66+ func render( _ ctx: LeafContext ) throws -> LeafData {
67+ let info = ctx. userInfo [ " info " ] as? String ?? " "
68+
69+ return . string( info)
70+ }
71+ }
72+
73+ let app = Application ( . testing)
74+ defer { app. shutdown ( ) }
75+
76+ app. views. use ( . leaf)
77+ app. leaf. configuration. rootDirectory = " / "
78+ app. leaf. cache. isEnabled = false
79+ app. leaf. tags [ " custom " ] = CustomTag ( )
80+ app. leaf. files = test
81+ app. leaf. userInfo [ " info " ] = " World "
82+
83+ app. get ( " test-file " ) { req in
84+ req. view. render ( " foo " , [
85+ " name " : " vapor "
86+ ] )
87+ }
88+
89+ try app. test ( . GET, " test-file " ) { res in
90+ XCTAssertEqual ( res. status, . ok)
91+ XCTAssertEqual ( res. headers. contentType, . html)
92+ XCTAssertEqual ( res. body. string, " Hello World! " )
93+ }
94+ }
5795}
5896
5997struct TestFiles : LeafFiles {
You can’t perform that action at this time.
0 commit comments