开发者

Simple Handmade PHP Template Engine not working! Help Please

These are the class definitions

<?php
  abstract class MyTemplate {

  protected $arrayOfSpaces;
  protected $arrayOfVariables;
  protected $output;

  protected abstract function __construct(); 

  function outputHTML(){
    echo $output; //Apparently, the problem is HERE. <<<<>>>>>
  }
}
  class MyTemplateMain extends MyTemplate {
   function __construct(){
     $this->output="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
             \"http://www.w3.org/TR/x开发者_如何学运维html1/DTD/xhtml1-transitional.dtd\">
             <html>
             <head>
             </head>
             <body>
             I love Rock n Roll!!!
             </body>
             </html>";
    }

  }
?>

And this is where I launch this page

<?php 
  require_once("view/templates.php");

  $page=new MyTemplateMain();
  $page->outputHTML();



?>

Doesn't work, though. Just shows a blank page, without the String I love rock n roll which was supposed to appear in the body.

I'm sure there are better ways to implement templates but I just want to figure out why this particular example doesn't work

Any help is appreciated. Thanks

PS: The quotes are all duly escaped and the file paths are ok too


change :

 function outputHTML(){
    echo $output;
  }

to :

 function outputHTML(){
    echo $this->output;
  }


your syntax is weird, try this

  abstract class MyTemplate {

  protected $arrayOfSpaces;
  protected $arrayOfVariables;
  protected $output;

  public abstract function __construct(); 

  function outputHTML(){
    echo $this->output;
  }
}
  class MyTemplateMain extends MyTemplate {
    public function __construct(){
     $this->output="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
             \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
             <html>
             <head>
             </head>
             <body>
             I love Rock n Roll!!!
             </body>
             </html>";
    }

  }

$page=new MyTemplateMain();
$page->outputHTML();


Ok. I don't know why but now it's working. I rewrote it some times so I must've done some correctly even though unintended. Here is the code if anyone wants to have alook and compare the versions. Thanks all

abstract class MyTemplate {

protected $arrayOfSpaces;
protected $arrayOfVariables;
protected $output;


function outputHTML(){
    echo $this->output;
}

}

class MyTemplateMain extends MyTemplate {

function __construct(){
    $this->output="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
             \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
             <html>
             <head>
             </head>
             <body>
             I love Rock n Roll!!!
             </body>
             </html>";
}
}


Try $base->output instead of $this->output.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜