88 */
99namespace Slim \Views ;
1010
11+ use \InvalidArgumentException ;
1112use Psr \Http \Message \ResponseInterface ;
1213
1314/**
@@ -23,14 +24,21 @@ class PhpRenderer
2324 */
2425 protected $ templatePath ;
2526
27+ /**
28+ * @var array
29+ */
30+ protected $ attributes ;
31+
2632 /**
2733 * SlimRenderer constructor.
2834 *
2935 * @param string $templatePath
36+ * @param array $attributes
3037 */
31- public function __construct ($ templatePath = "" )
38+ public function __construct ($ templatePath = "" , $ attributes = [] )
3239 {
3340 $ this ->templatePath = $ templatePath ;
41+ $ this ->attributes = $ attributes ;
3442 }
3543
3644 /**
@@ -58,6 +66,46 @@ public function render(ResponseInterface $response, $template, array $data = [])
5866 return $ response ;
5967 }
6068
69+ /**
70+ * Get the attributes for the renderer
71+ *
72+ * @return array
73+ */
74+ public function getAttributes ()
75+ {
76+ return $ this ->attributes ;
77+ }
78+
79+ /**
80+ * Set the attributes for the renderer
81+ *
82+ * @param array $attributes
83+ */
84+ public function setAttributes (array $ attributes )
85+ {
86+ $ this ->attributes = $ attributes ;
87+ }
88+
89+ /**
90+ * Get the template path
91+ *
92+ * @return string
93+ */
94+ public function getTemplatePath ()
95+ {
96+ return $ this ->templatePath ;
97+ }
98+
99+ /**
100+ * Set the template path
101+ *
102+ * @param string $templatePath
103+ */
104+ public function setTemplatePath ($ templatePath )
105+ {
106+ $ this ->templatePath = $ templatePath ;
107+ }
108+
61109 /**
62110 * Renders a template and returns the result as a string
63111 *
@@ -82,6 +130,12 @@ public function fetch($template, array $data = []) {
82130 throw new \RuntimeException ("View cannot render ` $ template` because the template does not exist " );
83131 }
84132
133+ foreach ($ data as $ k =>$ val ) {
134+ if (in_array ($ k , array_keys ($ this ->attributes ))) {
135+ throw new \InvalidArgumentException ("Duplicate key found in data and renderer attributes. " . $ k );
136+ }
137+ }
138+
85139 ob_start ();
86140 $ this ->protectedIncludeScope ($ this ->templatePath . $ template , $ data );
87141 $ output = ob_get_clean ();
0 commit comments