作成日:2006/08/07
4B.A 付録 ソースコード
本編で使用している例題(ss-tpl)の全てのソースコードは以下のサイトがらダウンロードできます。
Water-Sunlight※ただ今、準備中です。
http://www.y2sunlight.com/water/modules/mydownloads/
テンプレート: templates/theme.html
templates/theme.html
<{* Smarty Sample Template *}>
<{config_load file="meta.conf"}>
<!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" xml:lang="<{$site_lang}>"
lang="<{$site_lang}>">
<head>
<meta http-equiv="content-type"
content="text/html charset=<{$site_charset}>" />
<meta http-equiv="content-language" content="<{$site_lang}>" />
<meta name="robots" content="<{#meta_robots#}>" />
<meta name="keywords" content="<{#meta_keywords#}>" />
<meta name="description" content="<{#meta_description#}>" />
<meta name="rating" content="<{#meta_rating#}>" />
<meta name="author" content="<{#meta_author#}>" />
<meta name="copyright" content="<{#meta_copyright#}>" />
<title><{$page_title}></title>
<link rel="stylesheet" type="text/css" media="all" href="<{$site_css}>" />
<style type="text/css">
<!--
-->
</style>
<script type="text/javascript">
<!--
//-->
</script>
</head>
<body>
<div id="body">
<div id="header">
<h1><a href="<{$site_url}>"><{$site_title}></a></h1>
</div>
<div id="contents">
<{$contents}>
</div>
<div id="sidebar">
<ul>
<{if isset($menu)}>
<li><h3>Menu</h3>
<ul class="menu">
<{foreach item=menu_item from=$menu}>
<li><a href="<{$menu_item.url}>"><{$menu_item.title}></a></li>
<{/foreach}>
</ul>
</li>
<{/if}>
<li><h3>Special thanks</h3>
<ul>
<li><a href="#">Ground-Sunlight</a></li>
<li><a href="#">Water-Sunlight</a></li>
<li><a href="#">Wind-Sunlight</a></li>
</ul>
</li>
</ul>
</div>
<div id="footer">
<p>
Copyright (C) 2006
<a href="http://www.y2sunlight.com/">Y2Sunlight Office</a>
</p>
</div>
</div>
</body>
</html>
Smartyの派生クラス: MySmarty.class.php
MySmarty.class.php
<?php
require('Smarty/Smarty.class.php');
class MySmarty extends Smarty
{
function MySmarty ()
{
$this->Smarty();
$mydir = dirname(__FILE__);
$this->template_dir = "$mydir/templates/";
$this->compile_dir = "$mydir/templates_c/";
$this->config_dir = "$mydir/configs/";
$this->cache_dir = "$mydir/cache/";
array_unshift($this->plugins_dir, "$mydir/plugins/");
$this->debugging = false;
$this->force_compile = false;
$this->caching = false;
$this->left_delimiter = '<{';
$this->right_delimiter = '}>';
}
}
?>
※ 例題の環境では、PHPのインクルードパスの下に Smartyのインストールディレクトリ(Smarty/)があります。
プログラム: index.php
index.php
<?php
require('utils.php');
require('MySmarty.class.php');
$objSmarty =& new MySmarty;
# Setting ----------------------------------------
// サイト情報
$site_lang = 'ja'; // 使用言語
$site_charset = 'EUC-JP'; // 使用文字セット
$site_title = 'Smarty Sample Program'; // サイトタイトル
// コンテンツ
$contents[] = 'index'; // トップページ
$contents[] = 'news'; // ニュース
$contents[] = 'links'; // リンク集
# End Setting ------------------------------------
// サイト情報 ////////////////////////////////////
$site_url = dirname($_SERVER['SCRIPT_NAME']);
$site_css = $site_url.'/templates/style.css';
$objSmarty->assign('site_lang', $site_lang);
$objSmarty->assign('site_charset',$site_charset);
$objSmarty->assign('site_url', $site_url);
$objSmarty->assign('site_css', $site_css);
$objSmarty->assign('site_title', $site_title);
// メニュー //////////////////////////////////////
$conts_dir = dirname(__FILE__) . '/contents';
foreach($contents as $name)
{
$title = get_page_title( "$conts_dir/$name.html" );
$url = "$site_url/?p=$name";
$menu[] = array('title' => $title, 'url' => $url);
}
$objSmarty->assign('menu', $menu);
// コンテンツ ////////////////////////////////////
$page = 'index'; // Default
if(isset($_GET['p'])) $page = $_GET['p'];
$contents = file_get_contents("$conts_dir/$page.html");
$objSmarty->assign('contents', $contents);
// 出力 //////////////////////////////////////////
header('Content-Type:text/html; charset='.$site_charset);
$objSmarty->display('theme.html');
?>
MySmarty.class.php
<?php
require('Smarty/Smarty.class.php');
class MySmarty extends Smarty
{
function MySmarty ()
{
$this->Smarty();
$mydir = dirname(__FILE__);
$this->template_dir = "$mydir/templates/";
$this->compile_dir = "$mydir/templates_c/";
$this->config_dir = "$mydir/configs/";
$this->cache_dir = "$mydir/cache/";
array_unshift($this->plugins_dir, "$mydir/plugins/");
$this->debugging = false;
$this->force_compile = false;
$this->caching = false;
$this->left_delimiter = '<{';
$this->right_delimiter = '}>';
}
}
?>
※ 例題の環境では、PHPのインクルードパスの下に Smartyのインストールディレクトリ(Smarty/)があります。
