(3)PHPエッセイ---Smartyテンプレートエンジン技術基礎+MiniSmarty 01-02

5062 ワード

◇smartyはテンプレートエンジン技術の一つであり、「php」コードと「htmlコード」を分離する役割を果たす.
◇1つのプロジェクトには、プログラマー、アーティスト、DBAデータベース管理者、システムメンテナンススタッフがいます.
◇仮想ホストの作成、2つのステップ:
◇httpd-hostsを修正する.conf、.....ラベルの内容の変更
◇hostsファイルの最後の関連ドメイン名とipを変更します.すぐ
◇独自のテンプレートエンジンを作成するMiniSmarty.class.php、コードは以下の通りです.
 1 <?php
 2     class MiniSmarty
 3     {
 4         function compile($tpl){//        PHP  
 5             $cont = file_get_contents($tpl);
 6             //echo $cont;
 7 
 8             //    "{"  ---->     "<?php echo"
 9             $cont = str_replace("{","<?php echo ",$cont);
10             $cont = str_replace("}"," ; ?>",$cont);
11 
12             echo $cont;
13             //         (php+html    )       
14             file_put_contents('./tpl.html.php',$cont);
15         }
16 
17     }
18 ?>

◇phpファイルでhtmlファイルを処理する
 1 <?php
 2    $name = "xixi" ;
 3    $age = 20 ;
 4    $height = 180 ;
 5 
 6    require "MiniSmarty.class.php";
 7    $smarty = new MiniSmarty();
 8    $smarty -> compile('001.html');
 9    require_once "tpl.html.php";
10 ?>

◇このhtmlファイルの内容は:
 1 <html>
 2 <head>
 3 </head>
 4 <body>
 5     <div>{$name}</div>
 6 
 7     <div>{$age}</div>
 8 
 9     <div>{$height}</div>
10 </body>
11 </html>