0

This might seem really obvious but its not too me and I am looking to do something similar to what im trying to do.

Consider the following file -> sample.phtml which contains:

hello world.

If I do: $a = require_once('sample.phtml'); hello world is not captured in $a, instead the return value of require_once (in this case 1) is, and the contents of the file is echoed out to the screen.

What I am trying to do in what I call template injection. I want to capture the value of a template - html and all to then echo out into another template at some specific point.

So assume I have a template of:

   <div>
       ... fancy jazz
       <?php echo $a; ?>
   </div>

It is assumed that $a is whats called a view variable, that is a variable thats passed into the view via doing something like:

$$a = $a

I believe this considered a global variable?

Any how this is impossible because the contents of $a, are echoed above the content of the template, in this case above the top <div>.

Is there any way to do what I am trying to do?

TheWebs
  • 12,470
  • 30
  • 107
  • 211

1 Answers1

1

I believe this code will do what you want - start buffering the output, then capture the buffered output into a variable and delete the buffer. While my example simply immediately echoes the captured data, obviously you can do whatever you want with it.

<?php
    ob_start();
    require_once("afile.phtml");
    $a = ob_get_clean();
    echo $a;
Clyde
  • 7,389
  • 5
  • 31
  • 57