maor10
maor10

Reputation: 1784

Render PHP HTML page from variable

Say I have a variable of a string that contains an html page. Within that html page variable I have an echo statement- e.g. <?php echo 'whatever' ?>

Can I somehow "render" that variable to the browser, and have php evaluate all the php statements within the variable?

If I just echo the variable, the html renders fine but the php statements are not evaluated. If I try running eval on the entire page, it just throws an exception (makes sense).

I know this all sounds like bad practice, but I'm trying to figure out a way to do this without saving the variable to a file and loading the file.

BTW: I'm doing this all within codeigniter, so if there's a way to use $this->load->view on that variable... that would be even better :)

Example Code:

$x = /* Some logic to get a template data from another server */
/* $x is "<html><?php echo 'bla'; ?></html> */
echo $x;

This doesn't work- trying to run echo eval($x) also doesn't work

Upvotes: 0

Views: 3365

Answers (2)

DFriend
DFriend

Reputation: 8964

I'm not sure I follow you, but I think this is what you want.

If page1_view.php contains HTML and needs to echo a variable. e.g.

<div>Foo says: <?php echo $whatever; ?>!!!</div>

To get that "view" as a string with the variable $whatever evaluated you need this.

$view_data['whatever'] = "Hello World";
$page1 = $this->load->view('page1_view', $view_data, TRUE); 

$page1 now contains a string which is the contents of page1_view.php. in this case

"<div>Foo says: Hello World!!!</div>"

Upvotes: 0

zahorniak
zahorniak

Reputation: 135

You can write view data to variable and use this variable in other view

In you controller:

$data['view1'] = $this->load->view('my_view1', '', TRUE); // write view data to variable
$this->load->view('my_view2', $data);

In my_view2:

<html>
<?=$view1?>
</html>

This docs can help you Returning views as data - Codeigniter

Upvotes: 2

Related Questions