簡單講,就是一個PHP的template 官方網站
目地就是為了要讓html跟PHP做分隔
html部份主要是用BEGIN和END來做區塊的分格
HTML:
1 | <!-- BEGIN:main --> |
2 | <!DOCTYPE html> |
3 | < html lang = "zh-TW" > |
4 | < head > |
5 | < meta charset = "UTF-8" /> |
6 | < title >XTemplate Test</ title > |
7 | </ head > |
8 | < body > |
9 | <!-- BEGIN:block1 --> |
10 | < p >不會顯示</ p > |
11 | <!-- END:block1 --> |
12 | <!-- BEGIN:block2 --> |
13 | < p >顯示</ p > |
14 | <!-- END:block2 --> |
15 | <!-- BEGIN:show --> |
16 | < p >顯示陣列:{ROW.Name}的title是{ROW.Title}</ p > |
17 | <!-- BEGIN:threelayer --> |
18 | < p >來個第三層</ p > |
19 | <!-- END:threelayer --> |
20 | <!-- END:show --> |
21 | |
22 | <!-- BEGIN:string --> |
23 | {NULLSTRING} |
24 | <!-- END:string --> |
25 | |
26 | <!-- BEGIN:table --> |
27 | < table border = "1" > |
28 | < tr > |
29 | < td >姓名</ td > |
30 | < td >性別</ td > |
31 | </ tr > |
32 | <!-- BEGIN:table_row --> |
33 | < tr > |
34 | < td >{ROW1.Name}</ td > |
35 | < td >{ROW1.Gender}</ td > |
36 | </ tr > |
37 | <!-- END:table_row --> |
38 | </ table > |
39 | <!-- END:table --> |
40 | |
41 | </ body > |
42 | </ html > |
43 | <!-- END:main --> |
PHP:
1 | <?php |
2 | |
3 | //include XTemplate Class |
4 | include_once ( './xtemplate.class.php' ); |
5 | |
6 | //New XTemplate Object |
7 | $xtpl = new XTemplate( "xtpl_test.html" ); |
8 | |
9 | //如果建立新物件時沒有引入html,可以透過set_file設定 |
10 | $xtpl -> set_file( "xtpl_test.html" ); |
11 | |
12 | $arr = array ( |
13 | "Name" => "JohnsonLu" , |
14 | "Title" => "God" |
15 | ); |
16 | |
17 | $arr1 = array ( |
18 | array ( |
19 | "Name" => "Johnson" , |
20 | "Gender" => "男" |
21 | ), |
22 | array ( |
23 | "Name" => "Maple" , |
24 | "Gender" => "女" |
25 | ) |
26 | ); |
27 | |
28 | |
29 | //由內向外parse(沒有parse的話該區塊就不會顯示出來,最外層的main要最後parse) |
30 | $xtpl -> parse( "main.block2" ); |
31 | |
32 | //定義變數 |
33 | $xtpl -> assign( "ROW" , $arr ); |
34 | $xtpl -> parse( "main.show.threelayer" ); |
35 | $xtpl -> parse( "main.show" ); |
36 | |
37 | $xtpl ->set_null_string( "Hello EveryBody" , "NULLSTRING" ); |
38 | $xtpl -> parse( "main.string" ); |
39 | |
40 | //迴圈parse |
41 | foreach ( $arr1 as $val ){ |
42 | $xtpl -> assign( "ROW1" , $val ); |
43 | $xtpl -> parse( "main.table.table_row" ); |
44 | } |
45 | $xtpl -> parse( "main.table" ); |
46 | |
47 | $xtpl -> parse( "main" ); |
48 | //結束main |
49 | $xtpl -> out( "main" ); |
50 | |
51 | |
52 | ?> |