![Smartyインストール](https://lilia-study.com/wp-content/uploads/2020/11/smartyinst00.png)
社内向けのWebシステムをPHPで開発していますが、PHPのテンプレートエンジンはSmartyを使っています。
データを操作する部分はPHP、ブラウザに出力する部分はSmartyという感覚。
今回、Windows Server 2016にPHP7.4を設定したので、Smarty3.1のダウンロードと設定を記録します。
Windows Server 2016
PHP7.4
Smarty 3.1.35
IIS
ローカルネットワークです。
< スポンサーリンク >
Smartyのサイトのダウンロードページにアクセスします。
https://www.smarty.net/download
![Smarty設定](https://lilia-study.com/wp-content/uploads/2020/11/01.png)
少し下にスクロールします。
Smarty 3のバージョンの部分をクリックします。
![Smarty設定](https://lilia-study.com/wp-content/uploads/2020/11/02.png)
[Source code (zip)]をクリックします。
![Smarty設定](https://lilia-study.com/wp-content/uploads/2020/11/03.png)
ダウンロードしたzipファイルを解凍して、Web サイトのドキュメントルートのフォルダの中に「libs」フォルダを作り、その中に保存します。
※私の場合ドキュメントルートは、「C:\inetpub\wwwroot\」ではなく「D:\Webscript\」にしています。
![Smarty設定](https://lilia-study.com/wp-content/uploads/2020/11/04.png)
![Smarty設定](https://lilia-study.com/wp-content/uploads/2020/11/05.png)
php.iniを開き「include_path」の部分のコメントを外し「D:\Webscript\libs」を書き込み上書き保存します。
include_path = “.;D:\Webscript\libs”
![Smarty設定](https://lilia-study.com/wp-content/uploads/2020/11/06.png)
IISを再起動します。
![Smarty設定](https://lilia-study.com/wp-content/uploads/2020/11/07.png)
設定は終わりです。
Smartyの動作確認をしてみます。
PHPファイルを作成し、下記のように書きます。
<?php
require_once(“smarty-3.1.35/libs/Smarty.class.php”);
$smarty = new Smarty();
?>
![Smarty設定](https://lilia-study.com/wp-content/uploads/2020/11/08.png)
Webサーバーに今作成したPHPファイルをアップロードします。
![Smarty設定](https://lilia-study.com/wp-content/uploads/2020/11/08-2.png)
ブラウザでアップロードしたURLにアクセスしてみて、何も表示されなければSmartyが動作しています。
![Smarty設定](https://lilia-study.com/wp-content/uploads/2020/11/09.png)
設定が上手くいってない場合はエラーメッセージになります。
![Smarty設定](https://lilia-study.com/wp-content/uploads/2020/11/10.png)
文字を表示してみます。
「name」という変数に「あいう えお」という文字を代入し、ブラウザで表示してみます。
PHPファイル smarty_test.php
<?php
require_once(“smarty-3.1.35/libs/Smarty.class.php”);
$smarty = new Smarty();
$smarty->template_dir = “./templates/”;
$smarty->compile_dir = “./templates_c/”;
$smarty->config_dir = “./config/”;
$smarty->caching = “./cache/”;
$smarty->assign(‘name’,’あいう えお’);
$smarty->display(‘smarty_test.tpl’);
?>
![Smarty設定](https://lilia-study.com/wp-content/uploads/2020/11/smartyinst11.png)
tplファイル smarty_test.tpl
PHPファイルと同じフォルダに「templates」に作りその中に保存します。
<!DOCTYPE html>
<head>
<meta charset=”utf-8″>
<title>Smartyテスト</title>
</head>
<body>
<p>{$name}さん</p>
<p>こんにちは</p>
</body>
</html>
![Smarty設定](https://lilia-study.com/wp-content/uploads/2020/11/smartyinst12.png)
Webサーバーに今作成したPHPファイル、フォルダ、tplファイルをアップロードします。
同じフォルダ内に「templates_c」というフォルダを作っておきます。
![Smarty設定](https://lilia-study.com/wp-content/uploads/2020/11/smartyinst12-2.png)
ブラウザでアップロードしたURLにアクセスすると表示されます。
![Smarty設定](https://lilia-study.com/wp-content/uploads/2020/11/13.png)
こんな記事も書いています