Skip to content

Commit 31cea14

Browse files
committed
Allow to pass variables
1 parent 1423933 commit 31cea14

File tree

1 file changed

+51
-11
lines changed

1 file changed

+51
-11
lines changed

src/JsPhpize/JsPhpize.php

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ class JsPhpize
2828
*/
2929
protected $dependencies = array();
3030

31+
/**
32+
* @var array
33+
*/
34+
protected $sharedVariables = array();
35+
3136
public function __construct(array $options = array())
3237
{
3338
$this->options = $options;
@@ -92,8 +97,8 @@ public function compileCode($code, $catchDependencies = false)
9297
/**
9398
* Compile without the dependencies.
9499
*
95-
* @param string $input file or content
96-
* @param string $filename if specified, input is used as content and filename as its name
100+
* @param string $input file or content
101+
* @param string $filename if specified, input is used as content and filename as its name
97102
*
98103
* @return string
99104
*/
@@ -120,13 +125,18 @@ public function compileDependencies()
120125
/**
121126
* Compile and return the code execution result.
122127
*
123-
* @param string $input file or content
124-
* @param string $filename if specified, input is used as content and filename as its name
128+
* @param string $input file or content
129+
* @param string $filename if specified, input is used as content and filename as its name
130+
* @param array $variables variables to be used in rendered code
125131
*
126132
* @return mixed
127133
*/
128-
public function render($input, $filename = null)
134+
public function render($input, $filename = null, array $variables = array())
129135
{
136+
if (is_array($filename)) {
137+
$variables = $filename;
138+
$filename = null;
139+
}
130140
if (!in_array($this->stream, $this->streamsRegistered)) {
131141
$this->streamsRegistered[] = $this->stream;
132142
if (in_array($this->stream, stream_get_wrappers())) {
@@ -136,6 +146,7 @@ public function render($input, $filename = null)
136146
stream_wrapper_register($this->stream, $classParts[0] . '\Stream\ExpressionStream');
137147
}
138148

149+
extract(array_merge($this->sharedVariables, $variables));
139150
try {
140151
return include $this->stream . '://data;<?php ' . $this->compile($input, $filename);
141152
} catch (\JsPhpize\Compiler\Exception $e) {
@@ -156,24 +167,53 @@ public function render($input, $filename = null)
156167
/**
157168
* Render a file.
158169
*
159-
* @param string $file input file
170+
* @param string $file input file
171+
* @param array $variables variables to be used in rendered code
160172
*
161173
* @return string
162174
*/
163-
public function renderFile($file)
175+
public function renderFile($file, array $variables = array())
164176
{
165-
return $this->render(file_get_contents($file), $file);
177+
return $this->render(file_get_contents($file), $file, $variables);
166178
}
167179

168180
/**
169181
* Render raw code.
170182
*
171-
* @param string $code input code
183+
* @param string $code input code
184+
* @param array $variables variables to be used in rendered code
172185
*
173186
* @return string
174187
*/
175-
public function renderCode($code)
188+
public function renderCode($code, array $variables = array())
189+
{
190+
return $this->compile($code, 'source.js', $variables);
191+
}
192+
193+
/**
194+
* Add a variable or an array of variables to be shared with all templates that will be rendered
195+
* by the instance of Pug.
196+
*
197+
* @param array|string $variables|$key an associatives array of variable names and values, or a
198+
* variable name if you wish to sahre only one
199+
* @param mixed $value if you pass an array as first argument, the second
200+
* argument will be ignored, else it will used as the
201+
* variable value for the variable name you passed as first
202+
* argument
203+
*/
204+
public function share($variables, $value = null)
205+
{
206+
if (!is_array($variables)) {
207+
$variables = array(strval($variables) => $value);
208+
}
209+
$this->sharedVariables = array_merge($this->sharedVariables, $variables);
210+
}
211+
212+
/**
213+
* Remove all previously set shared variables.
214+
*/
215+
public function resetSharedVariables()
176216
{
177-
return $this->compile($code, 'source.js');
217+
$this->sharedVariables = array();
178218
}
179219
}

0 commit comments

Comments
 (0)