2014-02-13 21 views
-1

我有一個文件的template.php PHP代碼:如何運行的file_get_contents

<html> 
    <head> 
     <title>{title}</title> 
    </head> 
    <body> 
    <div id="header"> 
      <h1>This is header</h1> 
     </div> 
     <div id="content"> 
      <h1>This is content</h1> 
      <?php $this->showContent(); ?> 
     </div> 
    <div id="footer"> 
     <p>Copyright &copy; <?= date('Y'); ?> by My Company.</p> 
    </div> 
    </body> 
</html> 

,並在文件controll.php,代碼如下所示:

$file = file_get_contents('template.php'); 
echo $file; 

但不運行php腳本$this->showContent();

有人可以幫我嗎?

+0

您包含代碼文件,而不是使用'file_get_contents'的輸出 –

+0

這非常基本。在問這個問題之前,您可能需要閱讀PHP手冊。 – Capsule

+0

它似乎可能使用模板引擎。你知道'{title}'應該被替換嗎? – cornelb

回答

4

你應該嘗試

require_once('template.php'); // will execute the page as php 

什麼的file_get_contents將做的是閱讀文件爲某種文本文件,並返回它的價值。這不是你想要的。 require_once.php將包含php文件並將其作爲php處理。這與將文件中的代碼包含在其中相似。

+3

這個答案是正確的,但是爲什麼要使用這個解釋會改善這個答案的質量 –

+0

改進我的答案和解釋... –

相關問題