Smartyで「_index.tpl」が表示される問題を解決する方法

このQ&Aのポイント
  • Smartyを使用している際に、なぜか「index.tpl」ではなく「_index.tpl」が表示される問題が発生しています。どうすればこの問題を解決できるのでしょうか?
  • Smartyを利用している場合、表示されるべきテンプレートファイルが「index.tpl」ではなく「_index.tpl」となってしまう問題が発生しています。この問題を解決するためには、いくつかの方法があります。
  • Smartyを使用している際に、意図しないテンプレートファイル「_index.tpl」が表示される問題が発生しています。この問題を解決するためには、ファイル名の指定やファイルの存在確認などの対応が必要です。
回答を見る
  • ベストアンサー

Smarty で なぜか 「index.tpl」でなく「_index.tpl」が表示されてしまう

Smarty で、 //SMARTY define('SMARTY_DIR', $_SERVER["DOCUMENT_ROOT"] . '/../include/smarty/'); require_once(SMARTY_DIR . 'Smarty.class.php'); $o_smarty = new Smarty(); $o_smarty->template_dir = SMARTY_DIR . "templates/"; $o_smarty->compile_dir = SMARTY_DIR . "templates_c/"; $o_smarty->config_dir = SMARTY_DIR . "configs/"; //$o_smarty->cache_dir = SMARTY_DIR . "cache/"; //$o_smarty->assign("name","world"); $o_smarty->display("index.tpl"); のように書いているにも関わらず、 どうしても、 SMARTY_DIR . "templates/_index.tpl" のファイルが表示されてしまいます。(アンダースオア付き) それに、そもそも、「_index.tpl」 なんでファイルをつくったおぼえはありません。 また、smarty関連ファイルを「_index.tpl」でgrepしても、ひっかかりませんでした。 どうすれば、この状況を打破できるのでしょうか? よろしくお願いいたします。

  • PHP
  • 回答数1
  • ありがとう数6

質問者が選んだベストアンサー

  • ベストアンサー
回答No.1

コンパイルされたファイル(テンプレートをPHPに変換したもの)が、_index.tplに飛ぶようになっているかも知れません。 一度compile_dirの中にあるファイルを削除してみてはどうでしょうか?

daisuke_dm
質問者

お礼

ありがとうございます。 おっしゃるとおりでした。 削除したら、なおりました。 ただ、また同様の現象が再発するのではと懸念しています。 同様の現象が再発しないようには、どう対策すればいいのでしょうか? //なお、cacheとcomplieのちがいついていまいち理解できていません。 //これも。よろしければ教えていただけますか? よろしくお願いいたします。

関連するQ&A

  • ロリポップでSmartyのtpl読み込みエラー

    下記のコードを実行すると、  Smarty error: unable to read resource: "template1.tpl"  というエラーが出てしまいます。 $smarty->template_dirで指定したディレクトリ名の中に  "template1.tpl"というファイルは配置してあります。  原因などわかればおしえていただけないでしょうか。 index.php <?php require_once("Smarty.class.php"); $smarty = new Smarty; $smarty->template_dir = './templates/'; $smarty->compile_dir = './templates_c/'; $smarty->config_dir = './configs/'; $smarty->cache_dir = './cache/'; $smarty->display("template1.tpl"); ?> 【サーバ】  ロリポップです。 【その他】 index.phpをホームディレクトリ配下におくとエラーは消えます。 【ディレクトリ構成】 modules/index.php と templates/template1.tpl をがあり、 index.php から template1.tpl を読み込もうとしています。  ・modules →index.php ・templates →template1.tpl

    • ベストアンサー
    • PHP
  • Smartyについて

    Smartyについて Smartyをインストールしました。 OS:ubuntu インストール先:/usr/local/lib/Smarty-2.6.26 php.ini[include_path]の設定:/usr/local/lib/Smarty-2.6.26/libs/を追加 /var/www/以下にtestフォルダ作成 /var/www/test/index.html | /test/index.php chmod 644 | /tempalates chmod 775 templates | / index.tpl chmod 644 /templates_c chmod 775 templates_c /configs chmod 775 configs /cache chmod 775 cache index.phpの内容 <?php require_once(”Smarty.class.php"); $o_smarty=new Smarty(); $o_smarty->template_dir="./templates/"; $o_smarty->compile_dir="./template_c/"; $o_smarty->assign("name","world"); $o_smarty->display("hello.tpl"); ?> index.tplの内容 <html> <head> <title>Hello,world</title> </head> <body> Hello,{$name}! </body> </html> http://web.domain/test/index.php を入力するとエラーは出ませんが、何も表示されません。 これだけで、原因を特定出来ないのかもしれませんが、何処が悪いのでしょうか? 御教示お願いいたします。

  • Smartyに関しまして

    require_once("Smarty/Smarty.class.php"); $smarty = new Smarty(); $smarty->template_dir = "./templates"; $smarty->compile_dir = "./templates_c"; $smarty->assign("message", "こんばんは"); $a = array('hello','php','html'); $smarty->assign('a',$a); $smarty->display("index.tpl"); これでindex.tplで出力すると日本語部分が「?????」になってしまいます。 何か設定がいるんでしょうか??

    • ベストアンサー
    • PHP
  • tplを読みに行けません。

    PHP+smartyを学んでいます。 Apache2-htdocs-taro-hanako.php とファイルを置き、一方、 Apache2-php_libs-smarty の下に -libs/ (中にSmarty.class.php あり) と -templates/ (中にtest.tpl あり)を並列的におきました。 hanako.php に、 require_once "../../php_libs/smarty/libs/Smarty.class.php"; $smarty = new Smarty; $smarty->templete_dir = "../../php_libs/smarty/templates";   $smarty->compile_dir = "../../php_libs/smarty/templates_c"; $smarty->cache_dir = "../../php_libs/smarty/cache"; 以下省略・・・ と書いてブラウザで開くと、次のエラーが出て、tpl を読み込むことが出来ません。 Warning: Smarty error: unable to read resource: "test.tpl" in C:\Program Files\Apache Group\Apache2\php_libs\smarty\libs\Smarty.class.php on line 1088 どこを間違えているのでしょうか。

    • ベストアンサー
    • PHP
  • Smartyの設置について教えてください。

    お名前.COMにて提供しているVPSサーバプランについてですが、どなたかSmartyを設置された方いらっしゃいますか? いつもの通りユーザ階層にSmartyフォルダを設置し、一通りの事を行って、いざアクセスするもページが真っ白で表示されません。 エラーログを見ても普通にSmarty.classには到達しているようで、何が原因なのかわかりません(泣) てすとで通常のPHPページを作成し、現在の日時を表示するよう書き込んでアップしてみましたところ、普通に表示されるので普通にPHPは使えるんだなって感じです。 では、なぜSmartyだけうまく表示できないのでしょうか? 恐らく私の基本的なミスが原因だと思いますが、一応どなたか設置経験のある方がいましたら、設置内容を改めて教えていただければと思います。 どうぞ、宜しくお願いします。 ちなみに設置階層は下記の通りです。 /httpdocs/Smarty/libs/Smarty.class.php /httpdocs/data/cache /httpdocs/data/configs /httpdocs/data/templates /httpdocs/data/templates_c また、トップにあたるindex.phpの中は以下の通りです。 <?php require_once('./Smarty/libs/Smarty.class.php'); $smarty = new Smarty(); $smarty->template_dir = './data/templates/'; $smarty->compile_dir = './data/templates_c/'; $smarty->config_dir = './data/configs/'; $smarty->cache_dir = './data/cache/'; $smarty->display('index.tpl'); ?> アドバイスで構いませんので宜しくお願いします。

    • 締切済み
    • PHP
  • smartyのディレクトリ指定について

    お世話になります。 現在下記のような構成になってます d:\www web公開フォルダ d:\smarty テンプレート等のフォルダ smartyフォルダには templates_c templates cache configs があります。 wwwに置いてあるphpに $smarty->cache_dir = "D:/smarty/templates_c"; $Smarty->display("file:d:/www/templates/list.tpl"); と指定しましたがPHPのエラーログに Smarty error: the $compile_dir 'templates_c' does not exist, or is not a directory. と表示されてしまいます。 よろしくお願いします。

    • 締切済み
    • PHP
  • Smartyのtemplatesとtemplates_cのパスの基準について

    Windows環境でSmartyを動かしています。 動かしているサンプルソースがWindows用ではないため、パスなどを動くように直していたのですが、パスの設定でつまってしまいました。 サンプルのphpファイルはC:\xampp\htdocs\sample\に置いており、 Apacheのinclude_pathにC:\xampp\php\smarty\libs\を設定しました。 「templates_c」「templates」「configs」「cache」のフォルダを同じ場所に作成し、 それぞれディレクトリを指定しようとしたのですが、サンプルの「/home/~」というのをどのように直せばいいのか色々と試していて、 $smarty->template_dir = '../templates/'; $smarty->compile_dir = 'C:/xampp/php/smarty/templates_c/'; $smarty->config_dir = 'C:/xampp/php/smarty/configs/'; $smarty->cache_dir = 'C:/xampp/php/smarty/cache/'; で正常に動くのですが、 $smarty->template_dir = '../templates/'; $smarty->compile_dir = '../templates_c/'; とすると、templates_cが存在しないというエラーが出てきます。 compile_dirを"../"にするとhtdocsの直下にファイルができていたので、compile_dirはphpファイルからの相対パスで、template_dirはlibsからの相対パスになっているように思いますが、相対パスで指定する場合の基準パスが違うのでしょうか?

    • 締切済み
    • PHP
  • Smartyエラーについて

    いつもお世話になっております。 Smartyのエラーが解決できなくて困っています。 お手数ですが、後教授願えないでしょうか。 使用環境:Ubuntu Eclipse [エラー] Fatal error: Uncaught exception 'SmartyException' with message 'Unable to load template file 'list.tpl'' in /home/mobapendev/public_html/Smarty/sysplugins/smarty_internal_template.php:163 Stack trace: #0 /home/mobapendev/public_html/Smarty/sysplugins/smarty_internal_template.php(550): Smarty_Internal_Template->isExisting(true) #1 /home/mobapendev/public_html/Smarty/Smarty.class.php(338): Smarty_Internal_Template->getRenderedTemplate() #2 /home/mobapendev/public_html/Smarty/Smarty.class.php(382): Smarty->fetch('list.tpl', NULL, NULL, NULL, true) #3 /home/mobapendev/public_html/mobapen/list.php(20): Smarty->display('list.tpl') #4 {main} thrown in /home/mobapendev/public_html/Smarty/sysplugins/smarty_internal_template.php on line 163 ディレクトリ構成 Smarty/Smarty.class.php Project/list.php Project/php/common.php project/smarty/templates/list.tpl project/smarty/templates_c project/smarty/cache project/smarty/configs となっており、 common.phpにて、 $dir = getcwd(); $smarty->template_dir=$dir .'/smarty/templates'; $smarty->compile_dir=$dir .'/smarty/templates_c'; $smarty->config_dir=$dir .'/smarty/configs'; $smarty->cache_dir =$dir .'/smarty/cache'; list.phpにて require_onceで取り込みまいしたが、 $smarty->display("list.tpl"); ディレクトリは間違っていないとは思っているのですが、 どこでload出来てていなか分からず、攻め上げいている状況です。 以上、宜しくお願い致します。

    • ベストアンサー
    • PHP
  • Smartyでテンプレートの場所を指定する方法

    Smartyでテンプレートファイルの場所を指定するいい方法を教えてください。 公式マニュアルには基本的には(1)のようにやるのだと書いてあります。そして拡張セットアップとして(2)の方法が紹介されています。これなら「ディレクトリパスの設定を同じ変数に何度も割り当てる代わりに、一箇所でそれらを行う事が出来ます」とのことですが…。 しかし拡張セットアップの方法でも、プログラムファイル(.php)の中に環境依存の値を記述しています。私はJava開発の経験はあり、PHP開発の経験は無く、現在は趣味の範囲でPHPをやっています。 で、Javaの経験からすると、環境依存の値も設定ファイルのパスもプログラムファイルに書くことはまず無いので、(2)の方法でも気持ち悪く感じます…。また「ページを呼び出すたびに毎回setup.phpを呼ぶの?サーバー起動時に読み込むべき値・ファイルだろうが…」とも思ってしまいます。 …しかし私がどう感じるかは別として、実際の開発の現場ではどうすることが多いのか知りたくて質問をする事にしました。 質問は以下です。 [a]Smartyのテンプレートファイルのパスなどは、どこにどう書くことが多いのか? [b]Smartyに限らず、PHPという言語では、環境設定の値や設定ファイルのファイル名やパスを、プログラムファイル(.php)に書くのがあたりまえなのか?どうすることが多いのか。 [c]PHPではプログラムで使う値として、サーバー起動時に設定値・設定ファイルを読み込む一般的な仕組みがあるか?(サーバー設定値でもないのにphp.iniに書いてini_getで取得するなんてことが許される?他にもっとスマートな方法がある?) ※具体的な例を挙げて教えていただけると助かります。また、「あまりやらないけど、こうやったこともあるよ」などというイレギュラーなケースも教えていただけるとうれしいです。 よろしくお願いします。 --(1)index.php--------------------------------------------- <?php require_once(SMARTY_DIR . 'Smarty.class.php'); $smarty = new Smarty(); $smarty->template_dir = '/web/www.example.com/guestbook/templates/'; $smarty->compile_dir = '/web/www.example.com/guestbook/templates_c/'; $smarty->config_dir = '/web/www.example.com/guestbook/configs/'; $smarty->cache_dir = '/web/www.example.com/guestbook/cache/'; $smarty->assign('name','Ned'); $smarty->display('index.tpl'); ?> ----------------------------------------------------------- 基本的なインストール | Smarty http://www.smarty.net/docs/ja/installing.smarty.basic.tpl --(2)setup.php--------------------------------------------- <?php require('Smarty.class.php'); class Smarty_GuestBook extends Smarty { function __construct() { parent::__construct(); $this->template_dir = '/web/www.example.com/guestbook/templates/'; $this->compile_dir = '/web/www.example.com/guestbook/templates_c/'; $this->config_dir = '/web/www.example.com/guestbook/configs/'; $this->cache_dir = '/web/www.example.com/guestbook/cache/'; $this->caching = Smarty::CACHING_LIFETIME_CURRENT; $this->assign('app_name', 'Guest Book'); } } ?> --(2)index.php--------------------------------------------- <?php require('guestbook/setup.php'); $smarty = new Smarty_GuestBook(); $smarty->assign('name','Ned'); $smarty->display('index.tpl'); ?> ----------------------------------------------------------- 拡張セットアップ | Smarty http://www.smarty.net/docs/ja/installing.smarty.extended.tpl

    • ベストアンサー
    • PHP
  • Smartyでテンプレートが読み込めない

    下記のソースを実行すると、テンプレートが読み込めずエラーになってしまいます。 うーん、何故なのか分からず困っております。 phpのバージョンは4.4.6なのですが、Ver5以降でないとサポートされないのでしょうか。 [source] <?php require_once("Smarty.class.php"); class MySmarty extends Smarty { function _MySmarty() { $this->Smarty(); $this->template_dir="../templates"; $this->compile_dir="../templates_c"; } } $o_smarty=new MySmarty(); $o_smarty->display("hello.tpl"); ?> [error message] Warning: Smarty error: unable to read resource: "hello.tpl"

    • ベストアンサー
    • PHP

専門家に質問してみよう