From ebd1d41734a40967c08ce94651f3bd6f4ca27b52 Mon Sep 17 00:00:00 2001 From: Habu Date: Thu, 29 Mar 2018 19:16:23 +0900 Subject: [PATCH 1/1] =?utf8?q?[add]Web=E3=83=9A=E3=83=BC=E3=82=B8=E3=81=AE?= =?utf8?q?=E3=83=86=E3=83=B3=E3=83=97=E3=83=AC=E3=83=BC=E3=83=88=E3=82=92?= =?utf8?q?=E6=89=B1=E3=81=86=E3=82=AF=E3=83=A9=E3=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- score/web_template.inc | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 score/web_template.inc diff --git a/score/web_template.inc b/score/web_template.inc new file mode 100644 index 0000000..c63101e --- /dev/null +++ b/score/web_template.inc @@ -0,0 +1,91 @@ +template = file(self::$template_dir."/template.html"); + $this->head = file(self::$template_dir."/head.html"); + $this->header = file(self::$template_dir."/header.html"); + $this->footer = file(self::$template_dir."/footer.html"); + + $this->main_contents_fp = fopen('php://temp', 'r+'); + } + + /** + * Webページのメインコンテンツを書き込むリソースのポインタを得る + * + * @return resource Webページのメインコンテンツを書き込むリソースのポインタ + */ + public function main_contents_fp() + { + return $this->main_contents_fp; + } + + + /** + * Webページのhead内のtilteタグの内容をセットする + * + * @param string title セットするタイトルの文字列 + */ + public function set_title($title) + { + foreach ($this->head as &$line) { + $line = preg_replace('/.*<\/title>/', "<title>{$title}", $line); + } + } + + + /** + * Webページのhead内にコンテンツを追加する + * javasciprtやCSSを読み込ませるタグの追加に使用 + * + * @param string contents 追加するコンテンツの文字列 + */ + public function add_head_contents($contents) + { + $end_line = array_pop($this->head); + array_push($this->head, $contents."\n", $end_line); + } + + + /** + * Webページを出力する + */ + public function print_page() { + rewind($this->main_contents_fp); + $this->main_contents = [stream_get_contents($this->main_contents_fp)]; + foreach ($this->template as $line) { + echo $line; + switch (trim($line)) { + case "": + self::print_page_sub($this->head); + break; + case "": + self::print_page_sub($this->header); + break; + case "": + self::print_page_sub($this->main_contents); + break; + case "": + self::print_page_sub($this->footer); + break; + } + } + } + + private static function print_page_sub($lines) { + foreach ($lines as $line){ + echo $line; + } + } +} -- 2.11.0