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; } } }