2010-04-27 4 views
0

내 프로젝트에서 HTML의 출력을 내 프로그램 코드와 분리하여 매우 간단한 데이터베이스 클래스를 작성하고 싶습니다.내 PHP 템플릿 클래스에 대한 도움말

<?php 
class Template 
{ 
    private $template; 

    function load($filePath) 
    { 
     if(!$this->template = file_get_contents($filePath)) 
      $this->error('Error: Failed to open <strong>' . $filePath . '</strong>'); 
    } 

    function replace($var, $content) 
    { 
     $this->template = str_replace("{$var}", $content, $this->template); 
    } 

    function display() 
    { 
     echo $this->template; 
    } 

    function error($errorMessage) 
    { 
     die('die() by template class: <strong>' . $errorMessage . '</strong>'); 
    } 
} 
?> 

내가 도움이 필요한 것은 display() 방법입니다. 이 코드 사용 예를 들어 말 : 거기에 PHP 코드를 실행한다면 난 그냥 궁금 해서요

<html> 
    <head> 
     <title>{TITLE}</title> 
    </head> 
    <body> 
     <h1>{TITLE}</h1> 
     <?php 
      if($something) { 
       echo '$something is true'; 
      } else { 
       echo '$something is false'; 
      } 
     ?> 
    </body> 
</html> 

:

$tplObj = new Template(); 
$tplObj->load('index.php'); 
$tplObj->replace('{TITLE}', 'Homepage'); 
$tplObj->display(); 

그리고 index.php 파일은이 무엇입니까? 아니면 그냥 브라우저로 일반 텍스트로 보내겠습니까? 내 템플릿 클래스에서 eval()을 사용하고 있었지만 그 기능이 싫었습니다 : P

고마워요.

+1

왜 그냥 실행 해 보지 않으시겠습니까? – dclowd9901

+0

필자는 포함 된 파일에서 PHP를 실행하기 위해 사용해야하는 것에 대한 제안을 수집하고자했습니다. – blerh

답변

1

아니요. 일반 텍스트가 나옵니다.

'echo'는 PHP 코드를 구문 분석하지 않고 출력합니다.

eval을 사용하여 다시 폴백 할 필요가 없습니다. 예를 들어 출력 버퍼링을 사용하는 다른 방법이 있습니다.

관련 문제