1

How do I use variables from an included file, and use it for other included files as well?

index

<?php
$tmp = new template($connect);
$tmp->globals('index');
$logged_in = false; //works in all included files
?>
<html>
  <head>
    <?php $tmp->template('head'); ?> //class method to include file
  </head>
  <body>
    <?php echo $description; ?> //does not work either

include_head.php

 <title><?php echo $title; ?></title>//does not echo anything

index_globals.php

<?php
    $title="title";
    $description="description";   
 ?>

How I am including

public function template($file){
    if(isset($file) && file_exists($this->dir.$file.".php")){
        ob_start();
        include($this->dir.$file.".php");
        $template = ob_get_contents();
        return $template;
     }
}

Globals Function

public function globals($name){
  if(isset($name) && file_exists($this->dir.$name."_globals.php")){
      include($this->dir.$name."_globals.php");
  }
}
EasyBB
  • 6,176
  • 9
  • 47
  • 77

3 Answers3

1

You can "import" the globals by returning an array instead of declaring the variables:

<?php
// index_globals.php

return [
    'title' => 'title',
    'description' => 'description',
];

Then, from the globals() function you import it into a local property:

private $context = [];

public function globals($name)
{
    if (isset($name) && file_exists($this->dir.$name."_globals.php")) {
        $this->context = include($this->dir.$name."_globals.php");
    }
}

Finally, update the template() method:

public function template($file)
{
    if (isset($file) && file_exists($this->dir.$file.".php")) {
        extract($this->context);
        ob_start();
        include($this->dir.$file.".php");
        $template = ob_get_contents();
        return $template;
     }
}

Note that your index will not have access to $description in this case either, but it shouldn't be hard to gain access via the template instance.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • I edited your code you used `[]` when it should be `array()`. Other than that it worked like a charm! – EasyBB Jun 11 '14 at 06:15
  • 1
    @EasyBB The `[]` syntax has been around since 5.4, it's time to upgrade :) – Ja͢ck Jun 11 '14 at 06:15
  • PHP version 5.2.* damn 000webhost. Good thing I only use them for testing purposes and not to host my actual websites haa. Thank you so much Jack. You've helped me, plus you've taught me a very good little lesson here! Can't explain my appreciation! – EasyBB Jun 11 '14 at 06:16
1

You need to inject the variables into a stored property.

$tmp->set(array(
    'title' => 'hello world',
    'description' => 'this is the value'
));

// Or set a single value
$tmp->set('myCoolVariable', 'this is another value');

Implementation:

class template {
     protected $vars = array();

     public function set($key, $value)
     {
         if (is_array($key)) {
             // merge into existing
             $this->vars = array_merge($this->vars, $key);
         } else {
             // set a new variable with the name $key and value as $value
             $this->vars[$key] = $value;
         }
     }
}

Then in your output buffer method, extract() the stored variables

public function template($file)
{
    if (isset($file) && file_exists($this->dir.$file.".php")) {
        ob_start();
        extract($this->vars); // extract it so it is available for the current buffer
        include($this->dir.$file.".php");
        $template = ob_get_contents();
        ob_end_clean(); // don't forget to clean and turn it off
        return $template;
     }
}
vutran
  • 887
  • 8
  • 19
0

When you include a file in PHP using include() or require() or something that brings that file into another, the included file is basically embedded into the file including it. It's like writing all the code from the included file into the file calling include(*file*).

Simply put: When you include a file, all of it's variables are able to be used like any other variable declared if you successfully include it with include(), require(), or something like the mentioned methods.

Locke
  • 661
  • 1
  • 6
  • 17
  • Yeah, I've read that but it's not working for myself. I've also tested to see if that file is actually being parsed and it is – EasyBB Jun 11 '14 at 06:07