Skip to content

Instantly share code, notes, and snippets.

@wilf312
Created January 12, 2014 13:05
Show Gist options
  • Save wilf312/8384352 to your computer and use it in GitHub Desktop.
Save wilf312/8384352 to your computer and use it in GitHub Desktop.
自前のPHPテンプレート作ってみた。(Excel連携) ref: http://qiita.com/wilf312/items/472801d654a3d4c3a1a8
<!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>
<!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>
<?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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment