アプリ版:「スタンプのみでお礼する」機能のリリースについて

あるサイトで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.dt …
<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;
}
?>

A 回答 (1件)

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

    • good
    • 0
この回答へのお礼

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

お礼日時:2012/06/11 16:41

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!