ねこさんとへびさんの新人技術ブログ

新人エンジニアのねこさんとへびさんの、技術向上のためのブログです。

XAMPPでSmartyを使えるようにしてみた

XAMPPでSmartyを使えるようにしたので、手順書の覚書を残しておきます。

1.環境

XAMPP for Windows Version 1.7.3
PHP 5.3.1

諸事情により、色々と古い環境です;

2.Smartyをダウンロードする

こちらからSmartyをダウンロードします。
www.smarty.net

Smarty 3 latest を選択しました。

3.ダウンロードしたファイルのうちlibsフォルダ以下を移動

ダウンロードしたsmarty-3.1.30からlibsフォルダ

\xampp\phpの配下に移動します。

私は新しくincludesフォルダを作成し\xampp\php\includesの下にコピーしました。

4.テンプレート置き場を作成する

テンプレートファイルを格納する場所として、以下2つのフォルダが必要になります。

  • templates(テンプレートファイル)
  • template_c(コンパイルファイル)

お好きな場所に空フォルダを作成します。
私はこんな感じで。

\xampp\htdocs\project\templates 
\xampp\htdocs\project\template_c

今回はローカル環境なので適当に作成していますが、一般公開する際はセキュリティのためドキュメントルート外に置いた方が良いです。
(template_cは特に)

5.php.ini を編集する

smartyPHPに読み込ませる認定をします。
include_pathに先ほど移動したlibsフォルダのパスを追加
php.ini

include_path = ".;C:\xampp\php\PEAR"

を↓に変更

include_path = ".;C:\xampp\php\PEAR;C:\xampp\php\includes\smarty\libs"

6.smarty.class.php を編集する

smartyにテンプレート格納フォルダの場所を教えます。
$template_dir, $compile_dir のパスを変更する

smarty.class.php

protected $template_dir = array('/xampp/htdocs/project/templates/');
protected $compile_dir = '/xampp/htdocs/project/templates_c/';

7.動作確認

[PHP]
/xampp/htdocs/project/smarty_check.php

<?php
require('Smarty.class.php');

$smarty = new Smarty;

$smarty->assign('name', 'へびさん');
$smarty->display('index.tpl');
?>

[HTML]
拡張子をtplにして保存します。
設定を変えれば.htmlや.shtmlにすることも可能。
/xampp/htdocs/project/templates/index.tpl

<html>
<head>
<title>smarty</title>
</head>
<body>
<br />
{$name}さん、チャース!
</body>
</html>

画面を開いて「へびさん、チャース!」とsmarty変数が展開されているのを確認しておしまい。

蛇足
今回、目次記法というものを使ってみました。
なるほど便利~
staff.hatenablog.com

(byへびさん)