1

I'm creating my own Templating Script by using the ob_get_contents() as a core method. By using it, it can render out the other files, calling from the one single file.

Just like, lets assume we have 4 files:

  • index.php
  • header.html
  • footer.html
  • functions.php

index.php will call and render the contents of other files (2 html files here). By using the following codes:

//index.php
function render($file) {
    if (file_exists($file)) {
    ob_start();
    include($file);
    $content = ob_get_contents();
    ob_end_clean();
    return $content;
    }
}
echo render('header.html');
echo render('footer.html');

But (for example) when header.html contains a call include('functions.php'), the included file (functions.php) can't be used again in footer.html. I mean, i have to make a include again in footer.html. So here, the line include('functions.php') have to be containing in both of files.

How to include() a file without calling it again from child files?

夏期劇場
  • 17,821
  • 44
  • 135
  • 217

2 Answers2

1

When you use ob_start() (Output buffering), you end up only with the output of the file, meaning file executed the output is returned by ob_get_content(). As only output is returned that other file is unaware of the includes.

So the answer is: you can't do it with output buffering. Or include your files before ob_start with include_once.

Arman P.
  • 4,314
  • 2
  • 29
  • 47
  • include my files before `ob_start` with `include_once`? Oh, so for my example, it is possible if i declare all necessary files with `include_once` at the top of `index.php`. So the childs won't need to declare again? – 夏期劇場 Mar 21 '12 at 23:20
  • 1
    @4lvin Yes, of course it's possible. You can use `include_once` or `require_once` at the beginning and all your child files will see the included content. – Arman P. Mar 21 '12 at 23:22
  • WOw! Yeah! It is simple & incredible!! Cheers and thanks Arman P. – 夏期劇場 Mar 21 '12 at 23:34
1

That could work like something like this:

//index.php
function render($file) {
    if(!isset($GLOBALS['included'])) {
        $GLOBALS['included'] = array();
    } 

    if (!in_array($file, $GLOBALS['included']) && file_exists($file)) {
        ob_start();
        include($file);
        $content = ob_get_contents();
        ob_end_clean();

        $GLOBALS['included'][] = $file;
        return $content;
    }
}

echo render('header.html');
echo render('footer.html');

Alternatively you could use include_once (include_once $file;)and PHP will do it for you.

Though I suggest you just make sure the file loading structure is in such shape that these events never happen.

sg3s
  • 9,411
  • 3
  • 36
  • 52
  • Can i pls simply declare `include_once` one only time, at the very top of `index.php` file (before rendering the child files), instead of using your way `$GLOBALS`? For the knowledge please. – 夏期劇場 Mar 21 '12 at 23:26
  • Yes you can. But even better is making sure a file is never included twice no matter what file is called. – sg3s Mar 21 '12 at 23:28