Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use str_replace in Smarty

Tags:

html

php

smarty

I'm using PHP smarty to declare a link :

<{foreach item=list key=num from=$product}> 
    <li><a href="<{$config.weburl}>/<{$list.somename}>"><{$list.somename}></a></li>
<{/foreach}>`

and the resulting link when mouseover is :

"http:/domain/some name"

I need to str_replace the space char (some name) with an underscore (some_name), how to do that?? like result below :

"http:/domain/some_name"

I'm using the following code but it does not work. How to use str_replace with an array in html?

<a href="<{$config.weburl}>/'.str_replace(array(' ','%'),array('_','-'),<{$list.somename}>).'">
like image 205
prieku Avatar asked Jul 15 '14 08:07

prieku


1 Answers

You can use PHP functions as Smarty modifiers, but Smarty has a built-in replace modifier. Use it like this:

<{foreach item=list key=num from=$product}> 
    <li><a href="<{$config.weburl}>/<{$list.somename|replace:' ':'_'}>"><{$list.somename}></a></li>
<{/foreach}>
like image 111
Synchro Avatar answered Oct 26 '22 23:10

Synchro