views:

403

answers:

3

I'm using an MVC setup and I'm trying to inject javascript into my views (.php), yet allow the javascript access to all the variables that the view has access to. My end goal is to be able to access PHP variables from my javascript (for example so I could alert() a product's name).

Here's my application flow:

  1. start output buffer
  2. call extract() on an associate array (this contains the data for my view)
  3. call require on the view file
  4. return the contents of the output buffer, and clear the buffer
  5. echo buffer

I have a function injectJS(), which is as follows:

function injectJS($jsFile) {
 require APP_DIR . $jsFile;
}

Here's a sample of the Javascript (alert.js) I want to inject:

 <script type="text/javascript">
     alert("product name: <?php echo $product['name']; ?>");
 </script>

So, within my view file, I call injectJS('alert.js');. It injects the javascript, but I get a PHP notice of having an undefined variable $product when I render the view. However, if I write the require statement by hand, everything works fine. Is what I'm trying to achieve by using injectJS() possible?

Edit
I forgot to mention that I did try passing the variables explicitly to injectJS(), and it does work, but I don't want to have to always pass the variables. It could become messy & cumbersome if the view needs to pass many variables to the JS. Is there a way I could (within the function) grab the variables within the scope of the script responsible for calling the injectJS() ?

Edit 2
On second thought, passing the variables again is not a real issue, I think I'm just being greedy/lazy ;). I will just pass the variables when needed to injectJS(). Thanks for the responses

+2  A: 

Pass $product to your function and it will be visible inside the function body, or define it inside:

function globalJs( $filename, $product ) {
    include $filename;
}

globalJs( 'foo.js', array( 'name' => 'test' ) );
meder
+1  A: 

In PHP, variables are not visible inside functions, unless passed as parameters, or declared as global.

The problem you seem to have is that the $product variable doesn't exist inside the injectJS function ; if it's a global variable that's created outside that function, you should either pass it as a parameter, or declare it as global at the beginning of the function :

function injectJS($jsFile) {
    global $product;
    require APP_DIR . $jsFile;
}

For more informations, you can take a look at the section Variable scope in the manual, and, more specifically, the sub-section about The global keyword

Pascal MARTIN
+1  A: 

That will work just fine, but your scope problem is due to the variable not existing in your function. Try this:

function injectJS($jsFile) {
    $product = array('name' => 'test');
    require APP_DIR . $jsFile;
}

At some point you have to define the variable in your function's scope. The PHP global keyword will allow you to do this if the variable exists globally. Try to avoid using globals, though, since that leads to problems down the road.

Fragsworth