PHP(smarty)についての質問(文字化け)

このQ&Aのポイント
  • smartyを勉強中の方が、サンプルファイルの実行時に文字化けが発生して困っています。form.tplの日本語部分が文字化けし、文字コードがおかしいと思われます。
  • form.phpからお客様情報を入力させるためのテンプレートファイル(form.tpl)を指定し、template.tplにそれを表示させる処理を行っています。
  • form.tplに対しても文字コード(UTF-8)が有効になっていないと思われます。どのように修正すればよいでしょうか?
回答を見る
  • ベストアンサー

PHP(smarty)についての質問(文字化け)

あるサイトでsmartyを勉強中ですが、そのサンプルファイルを実行したところ、 文字化けが発生して困っています。 ・form.php←お問い合わせ用テンプレートファイルの指定 ・form.tpl←お問い合わせ情報を入力させるテンプレートファイル ・template.tpl←テンプレートファイルを表示させるテンプレートファイル form.phpから、お客様情報を入力させるためのテンプレートファイル(form.tpl)を指定し、 template.tplにそれを表示させるのが処理概要です。 実行すると、タイトルの"問い合わせフォーム"は表示されますが、 それ以下のform.tplの日本語部分が文字化けします。 template.tplで{include file=$content_tpl params=$params}の処理で form.tplを取り込んでいますが、その時文字コードがおかしくなっている気がします。 form.tplに対して文字コード(UTF-8)が有効になっていないと思われます。 実験的に、{include file=$content_tpl params=$params}を削除して、 そこにform.tplの記載をすべて記述すると文字化けは起こりませんでした。 文字コードの指定をform.tplに対しても有効にするにはどのように修正すればよろしいでしょうか。 以下にソースファイルを記載します。 ★form.php <?php // 共通の設定を読み込む require_once( 'common.php' ); // Smartyオブジェクト取得 $smarty =& getSmartyObj(); // ひな形のSmartyテンプレートでincludeするテンプレートを指定 $smarty->assign( 'content_tpl', 'form.tpl' ); // パラメータを渡す $smarty->assign( 'params', array( 'types' => array( 'request' => '意見、要望', 'question' => '質問、問い合わせ', 'other' => 'その他' ) ) ); // ページを表示する $smarty->display( 'template.tpl' ); ?> ★form.tpl <p>お問い合わせやご意見・ご要望を以下のフォームにご記入の上ご送信ください。</p> <form method="post" action="post.php"> <dl> <dt>お名前</dt> <dd> <input type="text" name="name" value="{$params.name|escape:'html':'UTF-8'}" /> {if $params.errors.name} <p class="error-message">名前を入力してください。</p> {/if} </dd> <dt>メールアドレス</dt> <dd> <input type="text" name="address" value="{$params.address|escape:'html':'UTF-8'}" /> {if $params.errors.address} <p class="error-message">アドレスが不正です。</p> {/if} </dd> <dt>お問い合わせ種類</dt> <dd> {html_options name="type" options=$params.types selected=$params.type} {if $params.errors.type} <p class="error-message">お問い合わせ種類が不正です。</p> {/if} </dd> <dt>お問い合わせ件名</dt> <dd> <input type="text" name="title" value="{$params.title|escape:'html':'UTF-8'}" /> {if $params.errors.title} <p class="error-message">件名を入力してください。</p> {/if} </dd> <dt>お問い合わせ内容</dt> <dd> <textarea name="content" cols="60" rows="5"> {$params.content|escape:'html':'UTF-8'} </textarea> {if $params.errors.content} <p class="error-message">お問い合わせ内容を入力してください。</p> {/if} </dd> </dl> <input type="submit" value="送信する" /> </form> ★template.tpl <?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>問い合わせフォーム</title> <style type="text/css"> {literal} .error-message{ margin:5px; font-size:80%; color:red; } {/literal} </style> </head> <body> <h1>問い合わせフォーム</h1> {include file=$content_tpl params=$params} </body> </html> ★common.php <?php define( 'SMARTY_DIR', 'C:/xampp\php/smarty/libs/' ); require_once( SMARTY_DIR .'Smarty.class.php' ); // Smartyオブジェクト取得 function & getSmartyObj() { static $smarty = null; if( is_null( $smarty ) ){ $smarty = new Smarty(); $smarty->template_dir = 'C:/xampp/php/smarty/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/'; } return $smarty; } ?>

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

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

  • ベストアンサー
  • singlecat
  • ベストアンサー率33% (139/418)
回答No.1

3種類のファイルのエンコードはどうなっていますか?

susumu_0414
質問者

お礼

あっ! なるほど、そういうことですか。 ファイル自体にエンコードがあることを知らなかった…です。 確かにtemplate.tpl以外Shift-JISになっていました。 UTF-8に統一すると文字化けが直りました。 有難うございます。。。うれしい。

関連するQ&A

  • 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
  • ロリポップで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のエラーが解決できなくて困っています。 お手数ですが、後教授願えないでしょうか。 使用環境: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動的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に関しまして

    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
  • 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のバージョンは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
  • 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
  • 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
  • Smarty3のリソース・プラグイン

    Smarty3のリソース・プラグインについて Smarty3(3.0.9)でリソース・プラグインを行うと以下エラーが出力され、データベース内のテンプレートを呼び出すことができません。 Uncaught exception 'SmartyException' with message 'Unable to load template db 'test.tpl'' in /usr/local/lib/php/smarty/sysplugins/smarty_internal_template.php:165 Stack trace: #0 /usr/local/lib/php/smarty/sysplugins/smarty_internal_template.php(554): Smarty_Internal_Template->isExisting(true) #1 /usr/local/lib/php/smarty/Smarty.class.php(340): Smarty_Internal_Template->getRenderedTemplate() #2 /usr/local/lib/php/smarty/Smarty.class.php(384): Smarty->fetch('test.tpl', NULL, NULL, NULL, true) #3 /usr/local/apache2/htdocs/test.php(18): Smarty->display('test.tpl') #4 {main} thrown in /usr/local/lib/php/smarty/sysplugins/smarty_internal_template.php on line 165 同様のプログラムでSmarty2*で実行するとうまく表示します。 Smarty3のマニュアル(英語版)を見てもうまくいきません。。 ご存知の方、ぜひ教えてください。 (環境) OS:Debian 6.0.1 WebServer:Apache2.2.19 PHP:PHP5.3.6 MySQL:MySQL5.0.87 (プラグイン) <?php require('DB.php'); define("DB_FILE","mysqli://*****:*****@localhost/*****"); function smarty_resource_db_source ($name, &$source, $o_smarty) { $db = DB::connect(DB_FILE); $stt = $db->prepare("SELECT source FROM templates WHERE name =?"); $rs = $db->execute($stt,array($name)); if(is_null($row=$rs->fetchRow(DB_FETCHMODE_ASSOC))){ return FALSE; }else{ $source=$row["source"]; return TRUE; } } function smarty_resource_db_timestamp($name, &$modified, $o_smarty) { $db = DB::connect(DB_FILE); $stt = $db->prepare("SELECT * FROM templates WHERE name =?"); $rs = $db->execute($stt,array($name)); if(is_null($row=$rs->fetchRow(DB_FETCHMODE_ASSOC))){ return FALSE; }else{ $modified=$row["modified"]; return TRUE; } } function smarty_resource_db_secure($name, $o_smarty) { // 全てのテンプレートがセキュアであると仮定します return true; } function smarty_resource_db_trusted($name, $o_smarty) { // テンプレートから使用しません } ?> (呼び出しファイル) <?php set_include_path('/usr/local/lib/php/smarty/:' . get_include_path()); @include('Smarty.class.php'); $o_smarty = new Smarty(); // Smartyのテンプレートのキャッシュファイル格納先を指定 $o_smarty->compile_dir = "/usr/local/apache2/htdocs/template_c"; $o_smarty->plugins_dir[]="/usr/local/apache2/htdocs/plugin"; $o_smarty->default_resource_type="db"; // phpスクリプトからテンプレートリソースを使用します $o_smarty->display("test.tpl"); ?> (データベース) CREATE TABLE templates ( name VARCHAR(100) NOT NULL default '', modified TIMESTAMP, source TEXT, PRIMARY KEY (name) ) DEFAULT CHARACTER SET utf8; -- Dumping data for table 'templates' INSERT INTO templates VALUES('test.tpl', "2010-12-25 22:00:00", '{$x="hello world"}{$x}');

    • 締切済み
    • PHP

専門家に質問してみよう