Question

I have this:

  ob_start();                     ?>
          <div class="" id="loading">
               <?php echo file_get_contents($page["texto"]) ?> 
          </div>      <?php

  $content = ob_get_clean();

But I would like to have that template html in a separate file, just like:

$content = file_get_contents('cms/template.php');

Now this won't work because it has php tags inside and when retrieving the string it gets as

how can I achieve this without using a dirty hack like:

$pre = file_get_contents('part1');
$var = file_get_contents($page["texto"]);
$post = file_get_contents('part2');

And adding all them...

Was it helpful?

Solution

It's still hack, but unless you use a templating system like Twig you have no choice:

ob_start();
include 'cms/template.php';
$content = ob_get_clean();

echo $content;

ob_start enables output buffering so nothing gets sent to the browser. We then include the file which will execute PHP normally. We then use ob_get_clean to get the contents of the output buffer (which is your template file). and disable the output buffer, discarding it's contents, as we have the contents in $content.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top