SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast C.Smith WebSites's Avatar
    Join Date
    Oct 2005
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple Template Script

    Hi, I'm creating a really basic template script and for some reason its not working and I cant see why.

    Heres my file with my template class in


    PHP Code:
    <?php
     
    class template {
     function 
    addvar($name$value) {
     
    $this->$name $value;
     }
     function 
    render($filename) {
     
    ob_start();
     include 
    $filename;
     
    $output ob_get_contents();
     
    ob_end_clean();
     echo 
    $output;
     }
     }
     
    $tpl = new template;
     
    $tpl->addvar("title""pagetitle");
     
    $tpl->render("templates/header.php");
     
    ?>
    Heres my header.php


    PHP Code:
    <html>
     <head>
     <title><?php $this->title ?></title>
     </head>
     <body>
     <h1><?php $this->title ?></h1>
     </body>
     </html>
    The problem is it outputs this


    HTML Code:
    <html>
     <head>
     <title></title>
     </head>
     <body>
     <h2></h2>
     </body>
     </html>
    So the variables aren't showing their values for some reason. Does anybody know why this isn't working?? Any help with this would be great.

    Thanks, Chris

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might wanna try this:

    <?php echo $this->title; ?>

    Notice echo there.

  3. #3
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by C.Smith WebSites
    Hi, I'm creating a really basic template script ...
    You have a massive security hole. "include $filename;" is extremely unsafe, as it potentially allows an attacker to open any file on your server. You need to edit this to limit the directories where templates can be included from.

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  4. #4
    SitePoint Enthusiast C.Smith WebSites's Avatar
    Join Date
    Oct 2005
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cmircea
    You might wanna try this:

    <?php echo $this->title; ?>

    Notice echo there.
    ohhhh thanks that makes sense. Is there a way to just have $this->title in the template on its own (without the echo or php tags) without it being outputted literally as $this->title but outputted as the desired "pagetitle" prefferably without using regex.
    Quote Originally Posted by seanf
    You have a massive security hole. "include $filename;" is extremely unsafe, as it potentially allows an attacker to open any file on your server. You need to edit this to limit the directories where templates can be included from.

    Sean
    Don't worry this was just a simple mock-up to see if the principle works. Thanks for your concern though.

    Chris

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2005
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by C.Smith WebSites
    Is there a way to just have $this->title in the template on its own (without the echo or php tags) without it being outputted literally as $this->title but outputted as the desired "pagetitle" prefferably without using regex.
    This should work:
    PHP Code:
    <?= $this->title ?>


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
  •