File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -40,6 +40,39 @@ $phpView = new PhpRenderer("./path/to/templates");
4040$response = $phpView->render(new Response(), "/path/to/template.php", $yourData);
4141```
4242
43+ ## Template Variables
44+
45+ You can now add variables to your renderer that will be available to all templates you render.
46+
47+ ``` php
48+ //Via the constructor
49+ $templateVariables = [
50+ "title" => "Title"
51+ ];
52+ $phpView = new PhpRenderer("./path/to/templates", $templateVariables);
53+
54+ //Or Setter
55+ $phpView->setAttributes($templateVariables);
56+
57+ //Or Individually
58+ $phpView->addAttribute($key, $value);
59+ ```
60+
61+ Data passed in via ` ->render() ` takes precedence over attributes.
62+ ``` php
63+ $templateVariables = [
64+ "title" => "Title"
65+ ];
66+ $phpView = new PhpRenderer("./path/to/templates", $templateVariables);
67+
68+ //...
69+
70+ $phpView->render($response, $template, [
71+ "title" => "My Title"
72+ ]);
73+ // In the view above, the $title will be "My Title" and not "Title"
74+ ```
75+
4376## Exceptions
4477` \RuntimeException ` - if template does not exist
4578
Original file line number Diff line number Diff line change @@ -86,6 +86,30 @@ public function setAttributes(array $attributes)
8686 $ this ->attributes = $ attributes ;
8787 }
8888
89+ /**
90+ * Add an attribute
91+ *
92+ * @param $key
93+ * @param $value
94+ */
95+ public function addAttribute ($ key , $ value ) {
96+ $ this ->attributes [$ key ] = $ value ;
97+ }
98+
99+ /**
100+ * Retrieve an attribute
101+ *
102+ * @param $key
103+ * @return mixed
104+ */
105+ public function getAttribute ($ key ) {
106+ if (!isset ($ this ->attributes [$ key ])) {
107+ return false ;
108+ }
109+
110+ return $ this ->attributes [$ key ];
111+ }
112+
89113 /**
90114 * Get the template path
91115 *
@@ -130,11 +154,15 @@ public function fetch($template, array $data = []) {
130154 throw new \RuntimeException ("View cannot render ` $ template` because the template does not exist " );
131155 }
132156
157+
158+ /*
133159 foreach ($data as $k=>$val) {
134160 if (in_array($k, array_keys($this->attributes))) {
135161 throw new \InvalidArgumentException("Duplicate key found in data and renderer attributes. " . $k);
136162 }
137163 }
164+ */
165+ $ data = array_merge ($ this ->attributes , $ data );
138166
139167 ob_start ();
140168 $ this ->protectedIncludeScope ($ this ->templatePath . $ template , $ data );
Original file line number Diff line number Diff line change @@ -26,22 +26,21 @@ public function testRenderer() {
2626 $ this ->assertEquals ("Hi " , $ newResponse ->getBody ()->getContents ());
2727 }
2828
29- /**
30- * @expectedException InvalidArgumentException
31- */
32- public function testExceptionForAttr () {
29+ public function testAttributeMerging () {
3330
3431 $ renderer = new \Slim \Views \PhpRenderer ("tests/ " , [
35- "hello " => "Hi "
32+ "hello " => "Hello "
3633 ]);
3734
3835 $ headers = new Headers ();
3936 $ body = new Body (fopen ('php://temp ' , 'r+ ' ));
4037 $ response = new Response (200 , $ headers , $ body );
4138
42- $ renderer ->render ($ response , "testTemplate.php " , [
39+ $ newResponse = $ renderer ->render ($ response , "testTemplate.php " , [
4340 "hello " => "Hi "
4441 ]);
42+ $ newResponse ->getBody ()->rewind ();
43+ $ this ->assertEquals ("Hi " , $ newResponse ->getBody ()->getContents ());
4544 }
4645
4746 /**
You can’t perform that action at this time.
0 commit comments