LoginSignup
7
6

More than 5 years have passed since last update.

【Smarty】配列のキーにハイフンを使ったらアウトらしい

Last updated at Posted at 2014-07-22

問題

PHPテンプレートエンジンのSmartyで、変数の内容がうまく表示されないという問題がありました。

それは、入力フォームで発生した問題。

form.html
 <input type="text" name="r1-date" value="{$post.r1-date}">
form.php
$post = $_POST;
$smarty->assign('post', $post);

入力欄には日付を"2014/07/22"の形式で入力し、ポストされた日付はvalue の値になる、という仕組みでした。

しかし、何度やっても出力が 0 (ゼロ)になる。。

どうしたもんか。

解決策

name とキーの "r1-date" から ハイフンを取り除いたら 解決しました。

form.html
 <input type="text" name="r1date" value="{$post.r1date}">
form.php
$post = $_POST;
$smarty->assign('post', $post);

けっこう長い時間つまっていたのですが、エディタのハイライト機能のおかげで「ハイフンがあやしい」と気づいて解決できました。

参考

解決した後に見つけた参考サイト。

http://babie.hatenablog.com/entry/20060306/p2
http://tohokuaiki.hateblo.jp/entry/20090525/1243228581

7
6
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6