1

I'm trying to display a lump of HTML text by a template variable and it seems to not be processing the PHP. I currently use this in my view (this is codeigniter)

<?php dump($_SESSION); echo (isset($html) && !empty($html)) ? $html : show_404(); ?>

The HTML includes a couple locations with php tags and I'd like those to get loaded when the page is echo'd. Is there a way to do this?

The main reason I need it to work from the HTML variable I pass in is because this is part of a page creator and I need to be able to plop php conditions in there that aren't in every page.

3
  • So the $html variable is a string which contains <?php ... code, and you want that code to be executed? Commented Aug 26, 2012 at 20:22
  • 1
    Sounds like you want to eval() something, which means you probably have something more fundamentally wrong with your design. eval() === √evil - do not do it! Commented Aug 26, 2012 at 20:24
  • isset($var) && !empty($var)!empty($var) - you don't need the redundant isset there. Commented Aug 26, 2012 at 20:36

3 Answers 3

1

There is one way to do it, but it's bad: eval()

echo (isset($html) && !empty($html)) ? eval($html) : show_404();
Sign up to request clarification or add additional context in comments.

2 Comments

eval will not work if the contents of $html is template-like (i.e. has <?php in it)
Someone upvoted what is considered an "evil" in programming and something that shouldn't work for this question? hmmm...
1

I would personally ob this from a file, buffering it and then use get_ob_clean to get the result, like so:

    ob_start();
        include 'temp_file_with_html_in';
        $view = ob_get_contents();
    ob_end_clean();

Note:

  • Your English is unclear so I am not sure exactly what you want
  • This may not work exactly as you want

Maybe a better explanation of what $html is and how it is sourced would help?

Comments

0

The quickest way I can think of (though a better solution could likely be found if I knew how $html was created) is to plop the contents into a tmp file and then include it. This is really a warning flag that there are architectural problems with the program, however (for instance, why can't the php interpolation be done when $html is created?)

$tmpFile = tempnam('/tmp', 'my_app_namespace');
file_put_contents($tmpFile, $html);
include $tmpFile;
unlink($tmpFile); // cleanup

edit, from Drupal 7

As you hinted in the comment, Drupal 7 does indeed contain what I would call a trick: prepending a PHP closing tag to the code you pass to eval, which will then handle it like a regular php include:

From Drupal 7's ./modules/php/php.module, around line 79, adapted a bit:

ob_start();
print eval('?>' . $html);
$output = ob_get_clean();

3 Comments

That could work, but I'm curious how Drupal does it. They use a similar thing with their article posting.
I found something interesting in Drupal 7, not sure if it's what they use in the feature you mentioned, but it might be more in line with what you were expecting...
Note, hoever, that Drupal's ./includes/ folder contains many ardent comments urging folks not to use eval...

Your Answer

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