OSDN Git Service

[modify]2段組みと1段組みの表で行ごとの色分けを区別
[hengband/web.git] / score / web_template.inc
1 <?php
2 /**
3  * Webページテンプレートを扱うクラス
4  * 使用方法:
5  * 1. インスタンス生成
6  * 2. 必要ならばset_title()でタイトル設定
7  * 3. 必要ならばadd_head_contentsでheadタグの内容を追加
8  * 4. main_contents_fp()で得たリソースにコンテンツを書き込み
9  * 5. print_page()でWebページ出力
10  */
11 class WebTemplate
12 {
13     private static $template_dir = "../template";
14
15     public function __construct()
16     {
17         $this->template = file(self::$template_dir."/template.html");
18         $this->head = file(self::$template_dir."/head.html");
19         $this->header = file(self::$template_dir."/header.html");
20         $this->footer = file(self::$template_dir."/footer.html");
21
22         $this->main_contents_fp = fopen('php://temp', 'r+');
23     }
24
25     /**
26      * Webページのメインコンテンツを書き込むリソースのポインタを得る
27      *
28      * @return resource Webページのメインコンテンツを書き込むリソースのポインタ
29      */
30     public function main_contents_fp()
31     {
32         return $this->main_contents_fp;
33     }
34
35
36     /**
37      * Webページのhead内のtilteタグの内容をセットする
38      *
39      * @param string title セットするタイトルの文字列
40      */
41     public function set_title($title)
42     {
43         foreach ($this->head as &$line) {
44             $line = preg_replace('/<title>.*<\/title>/', "<title>{$title}</title>", $line);
45         }
46     }
47
48
49     /**
50      * Webページのhead内にコンテンツを追加する
51      * javasciprtやCSSを読み込ませるタグの追加に使用
52      *
53      * @param string contents 追加するコンテンツの文字列
54      */
55     public function add_head_contents($contents)
56     {
57         $end_line = array_pop($this->head);
58         array_push($this->head, $contents."\n", $end_line);
59     }
60
61
62     /**
63      * Webページを出力する
64      */
65     public function print_page()
66     {
67         rewind($this->main_contents_fp);
68         $this->main_contents = [stream_get_contents($this->main_contents_fp)];
69         foreach ($this->template as $line) {
70             echo $line;
71             switch (trim($line)) {
72             case "<!--head-->":
73                 self::print_page_sub($this->head);
74                 break;
75             case "<!--header-->":
76                 self::print_page_sub($this->header);
77                 break;
78             case "<!--main contents-->":
79                 self::print_page_sub($this->main_contents);
80                 break;
81             case "<!--footer-->":
82                 self::print_page_sub($this->footer);
83                 break;
84             }
85         }
86     }
87
88     private static function print_page_sub($lines)
89     {
90         foreach ($lines as $line) {
91             echo $line;
92         }
93     }
94 }