php 模板寫法

來源:互聯網
上載者:User

<?php
/* @author: zhuyubing@gmail.com */
class Template{
        var $code;
        function Template($template){
            $this->code = implode('', @file($template));
        }
        function assign($name,$var=null){
    if(is_string($name) && is_string($var)){
     $this->code = str_replace('{'.$name.'}', $var, $this->code);
    } else if(is_array($var)){
     list($this->code,$tmp,$end)=explode('<!--'.$name.'-->',$this->code);
     while(list(,$v)=each($var)){
      $t=$tmp;$k2=$v2='';
      while(list($k2, $v2) = each($v)){
           $t = str_replace('{'.$k2.'}', $v2,$t);
      }
      $this->code .= $t;
     }
     $this->code .= $end;    
    } else {    
     while (list ($k2, $v2) = each($name)){
           $this->code = str_replace('{'.$k2.'}', $v2, $this->code);
     }
    }
        }
        function display(){
                echo $this->code;
        }
}
?>
最簡單的hello_world
準備一個php模版檔案hello_world.html
<html>
<head>
<meta http-equiv="Content-Type" c />
<title>{title}</title>
</head>
<body>
<h1>{title}</h1>
</body>
</html>
接下來就是模版的翻譯工作了
<?php
include('../include/template.php'); //包含模版核心類檔案
$tpl=new Template('hello_world.html'); //參數為模版路徑和檔案名稱,可以使用相對路徑,也可以使用絕對路徑
$tpl->assign('title',"hello world!"); //將標籤{title} 替換成hello world
$tpl->display();
?>

模版中使用數組
test_array.html
<html>
<body>
<h3>{user} {email}</h3>
<h3>{user1} {email1}</h3>
</body>
</html>
模版處理檔案
<?php
include('../include/template.php');
$tpl=new Template('test_array.html');
$user=array('user'=>'yubing','email'=>'test@sina.com');
$tpl->assign($user);
$tpl->assign('user1','jack');
$tpl->assign('email1','zhuyubing@gmail.com');
$tpl->display();
?>
簡單的區塊處理
block.html
<table width="400" border="1">
  <tr>
    <td>User Name</td>
    <td>E-Mail</td>
  </tr>
  <!--users-->
  <tr>
    <td>{name}</td>
    <td>{email}</td>
  </tr>
  <!--users-->
</table>
區塊處理常式
block.php
<?php
include('../include/template.php');
$tpl=new Template('block.html');

$users=array(
  array('name'=>'jack','email'=>'test@example.com'),
  array('name'=>'tom','email'=>'tom@sina.com')
  );  
$tpl->assign('users',$users);
$tpl->display();
?>
模版包含測試
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" c />
<title>{title}</title>
</head>
<body>
<h1>{title}</h1>
    <p>{block}</p>
</body>
</html>
模版包含處理常式
<?php
include('../include/template.php');
$tpl=new Template('block.html');
$users=array(
array('name'=>'jack','email'=>'test@example.com'),
array('name'=>'tom','email'=>'zhuyubing@gmail.com'),
);
$tpl->assign('users',$users);
$block=$tpl->code;
$tpl->Template('main.html');
$tpl->assign('block',$block);
$tpl->assign(array('title'=>'測試多模版檔案'));
$tpl->display();
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.