LoginSignup
13
12

More than 5 years have passed since last update.

Smartyで予約変数に可変変数を設定

Posted at

例えば配列で以下のデータがあります。
商品情報にタイトル用のフィールドが3つある。レコードは1つ。

{$arrProduct.title1}
{$arrProduct.title2}
{$arrProduct.title3}

これをせっせと{$arrProduct.title1}{$arrProduct.title2}{$arrProduct.title3}.........と全部書くのは大変なので、増えることも想定して変数を変数で設定したいと考えて調べた結果のメモ。

■3回回すサンプル。とりあえずリテラル値「3」を設定しているがそこも変数にするのが良い。

{section name=cnt loop=3}
  {var assing=str value="title"|cat:$smarty.section.cnt.iteration}
  {$arrProduct.$str}
{/section}

まぁ、可能な限りPHP側でやれという話なんですけど、Smarty側でどうしてもやらないといけない場合もあるので。

■参考サイト
http://www.smarty.net/docs/ja/language.modifier.cat.tpl

13
12
2

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
13
12