bicycle
bicycle

Reputation: 8430

smarty - replace multiple values instead of only one

documentation: http://www.smarty.net/docsv2/en/language.modifier.replace.tpl

Instead of replacing a single value, i would like to change multiple values. In the documentation it says the str_replace is the same as php's str_replace. Then how would i perform the following smarty wise?

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

Upvotes: 0

Views: 7512

Answers (1)

Pilznher
Pilznher

Reputation: 336

The equivalent in smarty would be:

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

Which gives the same output as your php:

apearpearle pear

But do be aware that "assignment of variables in-template is essentially placing application logic into the presentation that may be better handled in PHP" (taken from http://www.smarty.net/docs/en/language.function.assign.tpl )

Upvotes: 8

Related Questions