6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

自前のPHPテンプレート作ってみた。(Excel連携)

Last updated at Posted at 2014-01-12

HTML書き出すためのPHPのテンプレートはさっくり作ってみました。
Excelのデータと実際のHTMLを連携しつつ書き出すテンプレートになります。

Excelからデータの読み込みはこちらに書いたものをそのまま使用しています。
・ ExcelをPHPで読み込んで1行目を連想配列のキーにするクラス書いたよ。
http://qiita.com/wilf312/items/65643f2ee300cdabbdcd

使い道

文字コードはUTF8で作業して、
最後のHTML出力で文字コード一括変換かけるだけなので、意外と便利。
Analytics、計測タグのON,OFF、DB連携、Excel連携などなどカスタマイズ可能かと思います。

サンプルコード

template.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<!-- [[ PRINT PAGE DATA ]] -->
</head>
<body>
<!-- [[ PRINT NAVIGATION ]] -->
</html>

template.php
<?php

// ------ 設定関連
define( IS_SJIS , false);    // SJISに変換する場合はtrue
require path/to/excel.php; // Excelデータ取得クラス読み込み

// メタデータ作成関数
function getMetaData($url) { 

	// 全体の配列を取得する
	$metaData = EXCEL::getXls(dirname(__FILE__). '/myxls.xlsx', XLS_META);

	// 現在のページデータ取得
	$pageData = $metaData[$url];

	// HTMLの生成 (OGPが必要であればここに追記する)
	$metaHtml =<<<META
<title>{$pageData[‘keywords’]}</title>
<meta name="keywords" content=“{$pageData[‘keywords’]}” />
<meta name="description" content="{$pageData[‘keywords’]}" />
META;
	
	return $metaHtml;
}


// ------ 変換候補の設定
// key(HTMLコメント) → valueに置換 
$arrChange = array(
    '<!-- [[ PRINT NAVIGATION ]] -->'=> '<ul><li>about</li><li>items</li><li>contact</li></ul>',
    '<!-- [[ PRINT PAGE DATA ]] -->'=> getMetaData($_SERVER[SCRIPT_NAME])
);

// ------ テンプレートの取得
$template = file_get_contents(index_tpl.html);

// ------ 変換実行
$htmlData = strtr($htmlData, $arrChange);

// ------ 文字コード変換(必要あれば)
if (IS_SJIS) {
	$htmlData = mb_convert_encoding($htmlData, SJIS, UTF-8); 
}

// ------ 出力
file_put_contents('index.html', $htmlData);

index.html.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<title>トップ</title>
<meta name="keywords" content=“fugafuga1” />
<meta name="description" content=“hoge100” />
</head>
<body>
<ul><li>about</li><li>items</li><li>contact</li></ul><!— ここに出力 >
</html>

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?