Smartyのテンプレートにincludeしたファイルを表示する方法

このQ&Aのポイント
  • Smartyのテンプレートにincludeしたファイルの表示方法について説明します。
  • includeしたファイルが正常に表示された後、意図しない数値が表示される問題について解決策を提供します。
  • var_dumpで表示されるint(1)が問題の原因であることを指摘し、表示されないようにする方法を説明します。
回答を見る
  • ベストアンサー

Smartyのテンプレートにincludeしたファイルを表示したいので

Smartyのテンプレートにincludeしたファイルを表示したいのですが、うまくいきません。 具体的には、意図しない数値が入ってしまいます。 どうすればよいのでしょうか? ■呼び出す側 require("../../libs/Smarty.class.php"); require_once('../../lib/set_include_path.php'); $header = include("header.php"); $smarty = new Smarty(); $smarty->assign("header",$header); $smarty->display("date.html"); ■テンプレート <body> {$header}<br>日付<br> ■表示例 include("header.php"); の内容が正常表示 1 日付 include("header.php"); の内容が正常表示された後、勝手に「1」と表示されるので、困ってます。表示されないようにするにはどうすればよいでしょうか。 var_dump($header);ってしたところ、int(1) ってなるので、この1が表示されているのかと思うのですが、なんで表示されるのか、表示されないようにするにはどうすればよいか、教えてください。

  • re999
  • お礼率61% (476/777)
  • PHP
  • 回答数1
  • ありがとう数4

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

  • ベストアンサー
  • t_ohta
  • ベストアンサー率38% (5076/13261)
回答No.1

$header = include("header.php"); とした場合、$header の中に header.php の中身が代入されるわけではありません。 include はプログラムのその場所に指定されたファイルの中身を展開してしまいます。 この時 $header にはincludeの実行結果として成功した場合 1 が入り、失敗した場合は FALSE が入ります。 $header の中に header.php の中身を入れたい場合は $header = file_get_contents("header.php"); とするのがいいと思います。

re999
質問者

お礼

ありがとうございました。 うまく動作するようになりました。 大変勉強になりましたー。

関連するQ&A

  • smartyテンプレート読み込みについて

    Smarty動的Webサイト構築入門という書籍を見ながらSmartyを学習しているのですが、書籍のCD-ROMから落としたサンプルファイルを保存しているのですがどうしてもテンプレートを読み込むことができません。 03_01.php <?php require_once("../smarty/libs/Smarty.class.php"); $smarty = new Smarty(); $smarty->template_dir = "templates"; $smarty->compile_dir = "templates_c"; $smarty->assign("name", "Smartyさん"); $smarty->display("03_01.tpl"); ?> 03_01.tpl Hello, {$name}!! エラーメッセージ Warning: Smarty error: unable to read resource: "03_01.tpl" in C:\xampp\htdocs\smartybook\smarty\libs\Smarty.class.php on line 1092 03_01.phpは、C:\xampp/htdocs/smartybook/ren/に保存しています。 03_01.tplは、C:\xampp/htdocs/smartybook/templates/に保存しています。 どこが駄目なのか教えてください。 宜しくお願いします。

    • 締切済み
    • PHP
  • Smartyについて教えてください。

    Smartyについて教えてください。 参考書によってテンプレートの存在するディレクトリを指定している場合(1)と していない場合(2)がありますが、 どちらが正しいのでしょうか? (1) ********************************************* require_once('smarty/libs/Smarty.class.php'); $smarty = new Smarty(); $smarty->template_dir = "template02/"; $smarty->compile_dir = "compile/"; $smarty->display("content.tpl"); ********************************************* (2) ********************************************* require_once('smarty/libs/Smarty.class.php'); $smarty = new Smarty(); $smarty->display("content.tpl"); *********************************************

    • ベストアンサー
    • PHP
  • smarty3で変数表示について

    smarty3 でエラーが発生しています。 現在自宅のサーバーでsmarty3を勉強しているのですが以下のエラーで困っています。 詳しい方ご教授お願いします。 環境 : CentOS 6.3 PHP 5.3.3 Smarty 3.1.13 読み込んでいるファイル test.php 1 <?php 2 ini_set('display_errors', 1); 3 ini_set('date.timezone','Asia/Tokyo'); 4 define('SMARTY_DIR', 'Smarty/libs/'); 5 require_once( SMARTY_DIR. 'Smarty.class.php'); 6 7 $smarty = new Smarty(); 8 9 $smarty->template_dir = '../templates/'; 10 $smarty->compile_dir = '../templates_c/'; 11 $smarty->config_dir = '../config/'; 12 $smarty->cache_dir = '../cache/'; 13 14 $msg = "テストを表示します"; 15 $smarty->assign('msg', $msg); 16 17 $smarty->display('test.tpl'); 18 19 20 ?> テンプレートファイル 1 <html> 2 {$msg} 3 </html> 表示されるエラー文 Fatal error: Uncaught exception 'SmartyCompilerException' with message 'Syntax Error in template "../templates/test.tpl" on line 2 "{$msg}" unknown tag "private_print_expression"' in /usr/local/lib/php/includes/Smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php:665 Stack trace: #0 /usr/local/lib/php/includes/Smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php(451): Smarty_Internal_TemplateCompilerBase->trigger_template_error('unknown tag "pr...', 2) #1 /usr/local/lib/php/includes/Smarty/libs/sysplugins/smarty_internal_templateparser.php(2353): Smarty_Internal_TemplateCompilerBase->compileTag('private_print_e...', Array, Array) #2 /usr/local/lib/php/includes/Smarty/libs/sysplugins/smarty_internal_templateparser.php(3101): Smarty_Internal_Templateparser->yy_r27() #3 /usr/local/lib/php/includes/Smarty/libs/sysplugins/smarty_internal_templateparser.php(3201): Smarty_Internal_Templateparser->yy_reduce(27) #4 /usr/local/lib/php/includes/Smarty/libs/sysplugins/smarty_intern in /usr/local/lib/php/includes/Smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php on line 665 テンプレートファイルの{$msg}を削除して動作させるとコンパイルされてはいるようです。 よろしくお願いします。

    • 締切済み
    • PHP
  • Smarty includeでテンプレートを読み込めない

    よろしくお願いします。 まずphpファイルから $smarty->display('111/body.tpl');(*111はディレクトリ名) を呼び出して、さらにbody.tplと同ディレクトリにあるcontents.tplを読み込もうとしています。 はじめのbody.tplは読み込むことができブラウザで表示されるのですが、contents.tplは読み込めず、 Warning: Smarty error: unable to read resource: "contents.tpl" in <絶対パス>/Smarty/Smarty.class.php on line 1095 のエラーが出てしまいます。 ちなみに/111/というディレクトリは、$template_dir以下、$template_dir/111/です。 {include file="contents.tpl"} {include file="file:contents.tpl"} {include file="絶対パス/contents.tpl"} {include file="file:絶対パス/contents.tpl"} など試しましたが、ダメでした。 知恵をお貸しください。よろしくお願いします。

    • ベストアンサー
    • PHP
  • smartyの設定 include?path以下の設定がわからない

    独学でPHPを学んでいます。 PHPの基本はひととおり理解はできました。 開発環境・最新のXAMPP導入。 windows、OSはVISTA smartyをインストールしましたが、その設定につまずいています。 Smarty.class.phpが存在するパス(D:\xampp\libs)をphp.iniファイル447行目を下記のように設定しましたが、うまくいきません include_path=D:\xampp\libs\ xamppフォルダの下にlibsフォルダを入れたので上のパス指定で問題ないと思うのですが、どこが間違っているのでしょうか。 ちなみに、libs以下の\も不要だろうと思い、消したりして試してみましたが、結果は同じ。下記のように表示がなされます。 Warning: require_once(local/xampp/libs/Smarty.class.php) [function.require-once]: failed to open stream: No such file or directory in D:\xampp\htdocs\lesson\smarty1.php on line 3 Fatal error: require_once() [function.require]: Failed opening required 'local/xampp/libs/Smarty.class.php' (include_path='.;D:\xampp\php\pear\') in D:\xampp\htdocs\lesson\smarty1.php on line 3 上の文章ですが、3行目後半”Smarty.class.php' (include_path='.;D:\xampp\php\pear\')”の部分ですが、php.iniからinclude_pathを変更したりしているのですが、一向に表示が変わりません。 もしかしたらphp.iniファイルは二つあったりするのでしょうか。 php.iniのファイルはPHPフォルダに入っているものでよろしいですよね? もちろんアパッチは再起動させています。 なのにできません・ あと、ロジックファイルの require _once()の()内はSmarty.class.phpだけでもよろしいのでしょうか。 もう悩みすぎて頭がおかしくなりそうですね。 ちなみにsmartyがなくても、アプリケーション開発に支障はありませんでしょうか。 あくまで個人で趣味の範囲でアプリケーションを作成したいと感じています。

    • ベストアンサー
    • PHP
  • smartyのdifineについて

    表示するファイルにdefineにてパスを設定すると正常に 表示されるのですが管理が面倒なので別のファイルに defineにてパスを設定するとエラーになってしまいます。 具体的には以下のソースです。 ---Smarty/libs/path.php--- define ('SMARTY_LIBS_DIR', 'C:/Program Files/xampp/htdocs/Smarty/libs/'); ---Smarty/libs/MySmarty.class.php--- require_once dirname(__FILE__) . '/Smarty.class.php'; require_once dirname(__FILE__) . '/path.php'; ---Smarty/index.php--- require_once SMARTY_LIBS_DIR . 'MySmarty.class.php'; 調べてみたのですが表示するファイルに指定する方法しか みつからなかったのですが別ファイルから指定する事は 無理なのでしょうか?

    • 締切済み
    • 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について

    phpからassignしテンプレートに表示するには問題ないのですがテンプレートでassignし 表示させたく単純に以下のようにしてみましたがエラーになってしまいます。 <{assign var="aaa"value="名前"}> <{$aaa}> ※デリミタ設定は『<{』と『}>』で指定してます エラー内容は以下の通りです。 Fatal error: Smarty error: [in テンプレートファイル line assignしている行]: syntax error: invalid attribute name: '=' (Smarty_Compiler.class.php, line 1550) in ~中略~\Smarty\Smarty.class.php on line 1093 なんとなくではありますが'='が原因のような事はわかるのですが解決できずにおります。 この場合どうすればいいのでしょうか?

    • ベストアンサー
    • 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
  • 共通テンプレートを呼ぶ

    src/common/header.tplを src/hoge/hoge.tplから {include file="../common/header.tpl"} として呼びだしたい。 エラーメッセージが Warning: Smarty error: (secure mode) accessing "../common/header.tpl" is not allowed in C:\php\libs\Smarty.class.php on line 1088 です。 #### 設定 php.ini include_path = ".;C:\php\libs;C:\php\PEAR" safe_mode = Off Smarty.class.php $smarty->template_dir="."; {include file}を絶対パスで記述しても同じエラーです。 おそらくこのsecure modeの設定が正しく設定されていないのでしょうが その設定方法がいまいちよくわかりません。 簡単なことなのかもしれませんが、ご存知の方がいらっしゃいましたら ご教授願えないでしょうか。 よろしくお願いいたします。 環境 Windows XP php-5.0.5 Smarty-2.6.10

    • 締切済み
    • PHP

専門家に質問してみよう