0

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

3

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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>"

1 Comment

I don't have a file in my hand (what you called page1_view.php), I have it's contents as a variable ("<div>Foo says: <?php echo $whatever; ?>!!!</div> ")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.