X-Cart/Smarty Custom PHP Scripts

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cesarcesar
Forum Contributor
Posts: 111
Joined: Mon Oct 18, 2004 3:28 pm

X-Cart/Smarty Custom PHP Scripts

Post by cesarcesar »

i have what may be a very simple question for someone who has worked with X-Cart or Smarty.

I am adding a rollover image next to the category names in the main navigation. The following is my stripped down for this post version.

Code: Select all

       
{foreach from=$categories item=c}
 
     <div>
 
          <div><img name="{$c.category}_" src="{$ImagesDir}/custom/paw.gif"></div>
          <div><a href="home.php?cat={$c.categoryid}">{$c.category}</a></div>
 
     </div>
 
{/foreach}
 
I need to remove spaces in the names given at

Code: Select all

name="{$c.category}_"
I want to make it

Code: Select all

name="{str_replace(" ","_",$c.category)}_"
My attempts don't work and I get this error
Error: Smarty error: [in customer/categories.tpl line 26]: syntax error: unrecognized tag: str_replace(" ","_",$c.category) (Smarty_Compiler.class.php, line 436) in C:\apache2triad\htdocs\clients\sitename.com\www\Smarty-2.6.12\Smarty.class.php on line 1095
My question is.. How can I make PHP functions work in X-Cart/Smarty?

I cant even find where $c.category is set. Thanks much.
scriptah
Forum Commoner
Posts: 27
Joined: Sat Mar 15, 2008 8:58 pm
Location: Long Island, NY

Re: X-Cart/Smarty Custom PHP Scripts

Post by scriptah »

Smarty has its own function to replace occurrence of strings.

Code: Select all

 
 <div><img name="{$c.category}_" src="{$ImagesDir}/custom/paw.gif"></div>
 
Becomes:

Code: Select all

 
 <div><img name="{$c.category|replace:" ", "_"}_" src="{$ImagesDir}/custom/paw.gif"></div>
 
Which means, every space in $c.category will be replaced with "_".
cesarcesar
Forum Contributor
Posts: 111
Joined: Mon Oct 18, 2004 3:28 pm

Re: X-Cart/Smarty Custom PHP Scripts

Post by cesarcesar »

Thanks Scriptah. Here's the solution-

Code: Select all

{$c.category|replace:" ":""}
im now finding that slashes are not allowed either.

Code: Select all

{$c.category|replace:" ":""|replace:"/":""}
works too. sweet.
cesarcesar
Forum Contributor
Posts: 111
Joined: Mon Oct 18, 2004 3:28 pm

Re: X-Cart/Smarty Custom PHP Scripts

Post by cesarcesar »

how about this. I need to get the value of
{$c.category|replace:" ":""|replace:"/":""}
into a php variable. I thought it would be something like

Code: Select all

 
if ($_SERVER[QUERY_STRING] == 'cat=249') {
 
     $this->assign('HighPerformance','_ov');
 
}
 
<img src="{$ImagesDir}/custom/paw{${$c.category|replace:" ":""|replace:"/":""}}.gif">
but it is not working.
{${$c.category|replace:" ":""|replace:"/":""}} value is HighPerformance
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: X-Cart/Smarty Custom PHP Scripts

Post by RobertGonzalez »

Are you trying to set a view variable in the smary template? If so I think you can use the {assign var=varname value=value} method.
Post Reply