簡單講,就是一個PHP的template 官方網站

目地就是為了要讓html跟PHP做分隔

html部份主要是用BEGIN和END來做區塊的分格

HTML:

<!-- BEGIN:main -->
<!DOCTYPE html>
<html lang="zh-TW">
	<head>
		<meta charset="UTF-8" />
		<title>XTemplate Test</title>
	</head>
	<body>
		<!-- BEGIN:block1 -->
		<p>不會顯示</p>
		<!-- END:block1 -->
		<!-- BEGIN:block2 -->
		<p>顯示</p>
		<!-- END:block2 -->
		<!-- BEGIN:show -->
		<p>顯示陣列:{ROW.Name}的title是{ROW.Title}</p>
		<!-- BEGIN:threelayer -->
		<p>來個第三層</p>
		<!-- END:threelayer -->
		<!-- END:show -->
		
		<!-- BEGIN:string -->
		{NULLSTRING}
		<!-- END:string -->
		
		<!-- BEGIN:table -->
		<table border="1">
			<tr>
				<td>姓名</td>
				<td>性別</td>
			</tr>
			<!-- BEGIN:table_row -->
			<tr>
				<td>{ROW1.Name}</td>
				<td>{ROW1.Gender}</td>
			</tr>
			<!-- END:table_row -->
		</table>
		<!-- END:table -->
		
	</body>
</html>
<!-- END:main -->

PHP:

<?php
	
	//include XTemplate Class
	include_once('./xtemplate.class.php');
	
	//New XTemplate Object
	$xtpl = new XTemplate("xtpl_test.html");
	
	//如果建立新物件時沒有引入html,可以透過set_file設定
	$xtpl -> set_file("xtpl_test.html");
	
	$arr = array(
			"Name" => "JohnsonLu",
			"Title" => "God"
		);
	
	$arr1 = array(
			array(
				"Name" => "Johnson",
				"Gender" => "男"
			),
			array(
				"Name" => "Maple",
				"Gender" => "女"
			)
		);
	
	
	//由內向外parse(沒有parse的話該區塊就不會顯示出來,最外層的main要最後parse)
	$xtpl -> parse("main.block2");
	
	//定義變數
	$xtpl-> assign("ROW" , $arr);
	$xtpl -> parse("main.show.threelayer");
	$xtpl -> parse("main.show");
	
	$xtpl ->set_null_string("Hello EveryBody","NULLSTRING");
	$xtpl -> parse("main.string");
	
	//迴圈parse
	foreach($arr1 as $val){
		$xtpl -> assign("ROW1" , $val);
		$xtpl -> parse("main.table.table_row");
	}
	$xtpl -> parse("main.table");
	
	$xtpl -> parse("main");
	//結束main
	$xtpl -> out("main");
	
	
?>

Categories: PHP