0

I have the following code which sets a variable as the contents of a file and then uses that variable in the template file.

$content = file_get_contents('./section_body.php');

include $_SERVER['DOCUMENT_ROOT'] . '/includes/template.php';

This was working fine until I added an include into section_body.php. The contents of this include do not get rendered to the page.

Any ideas where I am going wrong?

Here's the code for section_body.php if it helps:

<?php 

include $_SERVER['DOCUMENT_ROOT'] . '/controllers/category_list.php';

?>

<div>
  <h1>Title</h1>
  <p>dfg kjdfsghsd fkjghdjksfg jdhksfg kjsdfg hjskdf ghjsdfg kdhjsfg </p>
</div>
Tom
  • 12,776
  • 48
  • 145
  • 240
  • 4
    file_get_contents does not execute php code , it just read the file as text or say binary. – anwerj Sep 08 '14 at 10:01
  • 1
    That makes sense! Can you point me the right way for a solution to the problem? Would I be right in thinking output buffering is the best way to go? – Tom Sep 08 '14 at 10:05
  • I think this answer is for you, [link](http://stackoverflow.com/questions/1272228/how-do-i-load-a-php-file-into-a-variable) – anwerj Sep 08 '14 at 10:14

2 Answers2

1

you can get rendered content as following without file_get_contents() so that you can reduce processing time.

 ob_start();
 include('./section_body.php');
 $content=ob_get_clean();
KyawLay
  • 403
  • 2
  • 10
0

It looks like your trying to build an template structure for your website. There are a lot existing, which would be also a choice.

You could work with the PHP output control functions, start with [ob_start()].(http://php.net/manual/en/function.ob-start.php)

<?php
    ob_start();
    include $_SERVER['DOCUMENT_ROOT'] . "section_body.php";
    $content = ob_get_contents();
    ob_end_clean();
    include $_SERVER['DOCUMENT_ROOT'] . '/includes/template.php';
?>
petres
  • 582
  • 2
  • 19