0

ドキュメント: http://www.smarty.net/docsv2/en/language.modifier.replace.tpl

単一の値を置き換える代わりに、複数の値を変更したいと思います。ドキュメントでは、str_replace は php の str_replace と同じであると書かれています。次に、次の賢い方法をどのように実行しますか?

$letters = array('a', 'p');
$fruit   = array('apple', 'pear');
$text    = 'a p';
$output  = str_replace($letters, $fruit, $text);
4

1 に答える 1

8

smarty で同等のものは次のようになります。

{assign "letters" array('a', 'p')}
{assign "fruit" array('apple', 'pear')}
{assign "text" 'a p'}
{$text|replace:$letters:$fruit}

あなたのphpと同じ出力が得られます:

apearpearle pear

ただし、「テンプレート内の変数の割り当ては、基本的にアプリケーションロジックをプレゼンテーションに配置することであり、PHP でより適切に処理できる可能性がある」ことに注意してください ( http://www.smarty.net/docs/en/language.function.assignから取得) 。 .tpl )

于 2013-10-01T15:52:33.850 に答える