トップページ > ホームページ作成 > templatesの変更
テンプレートエンジンのSmartyのディレクトリ「templates」等の名前や配置場所を変更する方法をお教えします。デフォルトのまま使用すると、悪意のある人から攻撃を受ける心配がありますので、なるべく変更して使いましょう。
Smartyを使う時に必要なディレクトリ
Smartyを使用する上で必要なディレクトリは、次の通りです。
- テンプレートファイル「**.tpl」を格納するディレクトリ「templates」
- コンパイル済みテンプレート格納ディレクトリ「templates_c」
- 直接呼び出せる出力内容をコピーしたファイルを格納する「cache」
(高速に動作できます)
これらのディレクトリは、ディフォルト設定ではテンプレートを実行する「phpファイル」を置くディレクトリの下に作ります。「templates_c」と「cache」のパーミッション(属性)は、(777)とします。
ディレクトリの名前や場所を変更する場合
ディレクトリの名前や場所を変更したサンプルリストです。
テンプレートファイルは、Smartyの使い方と同じ「smarty_test.tpl」を使用します。
次のように、各ディレクトリの場所とファイル名を変更しました。ディレクトリは、フルパスで指定する必要があります。また、「display」で表示する。テンプレートファイルは、「template_dir」にある場合には「template_dir」からの相対パスでもフルパスで指定できます。「template_dir」以外にある場合は、「file:」をつけフルパス(絶対パス)で指定する必要があります。
/home/sites/lolipop.jp/users/lolipop.jp-dp*********/web/
は、「ロリポップ」の場合のフルパスです。ディレクトリ名は、それぞれ「my_templates」「my_templates_c」「my_cache」とし、これらのディレクトリはフルパスの下のディレクトリ「my_smarty」に置いた場合の例です。
「cache」を有効にするには、「caching = true」で指定します。デバッグ時は、「false」を指定して、キャッシュを無効にします。
<?php
require_once("/usr/local/lib/php/Smarty/Smarty.class.php");
$smarty = new Smarty();
$smarty->template_dir = "/home/sites/lolipop.jp/users/lolipop.jp-dp*********/web/my_smarty/my_templates/";
$smarty->compile_dir = "/home/sites/lolipop.jp/users/lolipop.jp-dp*********/web/my_smarty/my_templates_c/";
$smarty->cache_dir = "/home/sites/lolipop.jp/users/lolipop.jp-dp*********/web/my_smarty/my_cache/";
$smarty -> caching = true;//キャッシングを有効にする:デバッグ時はfalseに設定
$smarty -> assign("value","こんにちは");
$smarty -> display("smarty_test.tpl");
//templete_dir以外(例えばmy_templates2)のテンプレートファイルを呼ぶ場合
//$smarty -> display("file:/home/sites/lolipop.jp/users/lolipop.jp-dp*********/web/my_smarty/my_templates2/smarty_test.tpl");
?>
レンタルサーバーの「ロリポップ」では、動作確認しております。
Amazon.co.jpアソシエイト:PHP&プログラミング
:%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0%20php :php2_ :3