Problems output from parsed template

I apologize for such rudimemtary questions, but I am returning to PHP after a nearly five years away.

I have some html files with embedded php variables in them. Here are some stripped down samples of the actual code, as it is right now.

App.php


<?php
class App
{
 public static function Initialize()
 {
  $sid = session_id();
  if (empty($sid))
  {
   session_start();
  }
  
  ob_start('ob_tidyhandler');
  Skin::Header('test title');
 }
 public static function Process()
 {
  Skin::Error('one is lonely number');
 }
 public static function Terminate()
 {
  Skin::Footer();
  ob_end_flush();
 }
}
?>

Skin.php

<?php
class Skin
{
public static function Render($file, $data)
{
extract($data);
$html = file_get_contents(sprintf('skin/%s.html', $file));
eval("\\$html = \\"$html\\";");
echo($html);
}
public static function Header($title)
{
Skin::Render(
'header',
array(
'title' => $title
)
);
}
public static function Footer()
{
Skin::Render(
'footer',
array()
);
}
public static function Error($message)
{
Skin::Render(
'error',
array(
'message' => $message
)
);
}
}
?>

error.html

<div id="error">
<p>{$message}</p>
</div>

The php error I get is:

Parse error: syntax error, unexpected ‘<’ in /home/drew/public_html/FlashCards/StandAlone/core/Skin.php(13) : eval()'d code on line 2 Parse error: syntax error, unexpected T_STRING in /home/drew/public_html/FlashCards/StandAlone/core/Skin.php(13) : eval()'d code on line 1

Now, don’t bother asking why the functions are all static, there’s a reason, and YES, I know eval is frowned upon, but I can’t see any other way to do this. If there is a better way, please let me know. Otherwise, why doesn’t the above work?

And how the heck do you get indents to work when posting???

There’s a better way, but that involves using <?php tags instead, and using include.

I believe your indent problem is because you have wysiwyg formatting on? That gave me no end of problems, since an upgrade a few months back I believe. There should be a button that looks like [sup]A[/sup]/[u]A[/u], click that and it should turn it off. That’s not permanent though, you’ll need to modify your forum settings to disable wysiwyg.

I believe your problem is that your file contains slashes, right? So it looks like the following to your parser:

$html = "<div id="error">
<p>{$message}</p>
</div>";

Which, as you can see, won’t parse. Simplest method is to run addslashes() on your $html before eval()ing it so that it works.

For the record, eval is ‘bad’ because people use it badly. You use it just fine - It’s about the same as an include there. Truthfully I’d rather an ‘include’ version, but you may as well stick to what you’re comfortable with. The only problem is it limits you to only inserting variables: you can’t (without a serious bodge) throw any logic into it. Views are meant to have logic in, because they control what is being viewed. But if you are happy with how you have it, keep it. It’s your code after all!

Thanks. One thing I found out later is that the use of {} isn’t going to work anyway. It’s fine in heredoc style things, but in this case, I had to use <?=$message?> instead. Yeah, I really don’t want to have to type out the <?php echo $message?>. And this is really a very simple app, it isn’t a full blown mvc type thing. Thanks though!