テンプレートや設定ファイルをドキュメントルートの外に配置することで、Webブラウザからこれらのファイルが見えなくなります。
方法は、Smartyクラスのプロパティ$template_dir, $compile_dir, $config_dir, $cache_dirに場所を設定します。
また、この設定はSmartyを使用する各アプリケーションごとに区別して設定する事が推奨されています。
下記の場所に配置することにします。
─┬─ | htdocs/ | ─┬─ | index.html | |||||
│ | └─ | SampleApp/ | ─── | index.php | ||||
└─ | Smarty/ | ─── | SampleApp/ | ─┬─ | templates/ | ── | index.tpl | |
├─ | templates_c/ | |||||||
├─ | configs/ | |||||||
└─ | cache/ |
Smartyが動作するか確認プログラムを修正します(下記を入力したファイルindex.phpを作成)。
<?php require_once('Smarty.class.php'); // create object $smarty = new Smarty; // smarty config(template cache ...) $smarty->template_dir= '../../Smarty/SampleApp/templates/'; $smarty->compile_dir = '../../Smarty/SampleApp/templates_c/'; $smarty->config_dir = '../../Smarty/SampleApp/configs/'; // $smarty->cache_dir= '../../Smarty/SampleApp/cache/'; // assign some content. $smarty->assign('name', 'sagasaga'); $smarty->assign('path', ini_get('include_path')); // display it $smarty->display('index.tpl'); ?> |
実際のアプリケーションでは、さらにフォルダ分けをおこなうのが普通です。
Smarty/ | ─── | SampleApp/ | ─┬─ | templates/ | ─┬─ | applicationAA/ | ─ | aa.tpl |
│ | ├─ | applicationBB/ | ─ | bb.tpl | ||||
│ | ├─ | applicationCC/ | ─ | cc.tpl | ||||
│ | └─ | ..... | ||||||
├─ | templates_c/ | |||||||
├─ | configs/ | |||||||
└─ | cache/ |