SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)

    Question 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 Code:
    <?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 Code:
    <?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
    HTML Code:
    <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???

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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 A/A, 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:
    PHP Code:
    $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!
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    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!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •