2012-01-25 18 views
1

如何將HTML模板轉換爲活動的PHP可編輯模板?我希望能夠設置標題,說明,添加圖像和鏈接。 (類似behance上傳系統)如何使用PHP將HTML轉換爲實時可編輯模板?

有人可以將我鏈接到教程嗎? 非常感謝!

+1

這要麼太普通,要麼你需要學習php的基礎知識。查看「CMS」框架,如wordpress –

+0

我敢打賭,Google可以將您鏈接到比我更快的教程:) – rdlowrey

+0

嘗試使用CKEditor或任何其他所見即所得編輯器 –

回答

2

要以簡單的方式做到這一點,就應該按照這個3個步驟:1 添加自定義代碼會在HTML模板 2 - 創建一個類來讓你的HTML模板寫 3-加載類,你寫模板,顯示頁面

  1. 首先,將你的HTML模板(template.html)一些自定義的標籤,就像這樣:

  2. 然後,創建一個快捷類(class.php),讓您的自定義標籤可寫:

    class Template { var $contents; 
    
    
        function load($file) { 
         if ($fp = fopen($file, "r")) { 
          $this->contents = fread($fp, filesize($file)); 
          fclose($fp); 
         } 
        } 
    
        function replace($str,$var) { 
         $this->contents = str_replace("<".$str.">",$var,$this->contents); 
        } 
    
        function show() { 
         $search = array(
           '/\t/', //Remove Tabs 
           '/<!--[^\[-]+?-->/', //Remove Comments 
           '/\n\n/' //Remove empty lines 
           ); 
          $replace = array(
           '', 
           '', 
           '' 
           ); 
         $this->contents = preg_replace($search, $replace, $this->contents); 
         echo $this->contents; 
        } 
    } 
    

一旦做到這一點,你必須創建一個函數裏面寫您的標籤。在我的例子,要能寫你<page_title>標籤,下面的代碼添加到您的class.php文件:

function writetitle($s) { 
    $GLOBALS['writes']++; 
    $GLOBALS['page_title'] .= $s; 
    return; 
} 
  1. 最後,所有你需要的是做的是創造你的page.php文件。加載班級,寫出你想要的,並顯示結果。

喜歡的東西:

<?php 
    require_once('class.php'); //Load Class 
    $template = new Template;  
    $template->load("template.html"); //Load HTML template 

    //Some query : 
    $query = mysql_query('SELECT...'); 
    $res = mysql_num_rows($query); 

    writetitle('my page title went live!, '.$res.''); //write your content 


    $template->show(); //Generate the page 
?> 

writetitle現在充當呼應,這樣你就可以讓你想查詢的一切。

你到底有3個文件: tempalte.html:你的模板 class.php:你的模板引擎 page.php文件:一個示例頁面usging您的模板。

希望它有幫助。 ;)

0
  1. 保存.html文件作爲.PHP
  2. 在你的PHP,加載要填充模板(這是一個很大的話題,以及基礎學習PHP的數據,不是我能回答你在SO上)
  3. 將你加載的變量回聲到你的模板中。初學者的方法就是這樣。在 「使用example.php」:

<?php 
//1. Populate the value from PHP somehow, such as from the GET variables in your HTTP request 
$personsNameFromPHP = $_GET['personsNameFromGETVariables']; 

//2. Echo the variable out in your markup like this: 
?> 
<div class="personsName"> 
<?php echo $personsNameFromPHP; ?> 
</div> 

W3Schools的是一個體面的起點。如果您已經瞭解了PHP語法,請先了解MySQL數據庫的工作原理以及PHP如何訪問它們。如果你不知道PHP,W3Schools也有一些鏈接。

http://www.w3schools.com/PHP/php_mysql_intro.asp

HTH。