Page 1 of 1

X-Cart/Smarty Custom PHP Scripts

Posted: Mon Mar 17, 2008 12:42 am
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.

Re: X-Cart/Smarty Custom PHP Scripts

Posted: Mon Mar 17, 2008 12:58 am
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 "_".

Re: X-Cart/Smarty Custom PHP Scripts

Posted: Mon Mar 17, 2008 1:11 am
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.

Re: X-Cart/Smarty Custom PHP Scripts

Posted: Mon Mar 17, 2008 6:24 pm
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

Re: X-Cart/Smarty Custom PHP Scripts

Posted: Mon Mar 17, 2008 6:31 pm
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.