X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Changing design (https://forum.x-cart.com/forumdisplay.php?f=51)
-   -   Incrementally number in a Smarty template (https://forum.x-cart.com/showthread.php?t=71615)

MAPerformance Admin 03-13-2015 10:28 AM

Incrementally number in a Smarty template
 
We are trying to put a conversion script in our order_message.tpl file, but we need to increment a number in the script for each item. I am familiar with {foreach} but do not know how to do this specific task.

*************************
Base Script Example
*************************
<script type="text/javascript">
var productDetailsObj = {ldelim}
{foreach from=$orders.0.products item=prod}
'1' : {ldelim}
'id' : {$prod.productid},
'price': {$prod.price},
'qty': {$prod.amount}
{rdelim},
{/foreach}
{rdelim};
</script>
*************************
End Base Script Example
*************************

See that '1' in the script, directly under the {foreach} statement? That's the number I need to increment. Here's an example of a working script rendered output:

<script type="text/javascript">
var productDetailsObj = {
'1' : {
'id' : 12345,
'price': 49.99,
'qty': 1
},
'2' : {
'id' : 23456,
'price': 19.99,
'qty': 4
},
'3' : {
'id' : 34567,
'price': 109.99,
'qty': 2
},
};
</script>

totaltec 03-13-2015 11:05 AM

Re: Incrementally number in a Smarty template
 
I think you can make use of the iteration property. To do this I believe your foreach loop needs a name:
Code:

{foreach from=$orders.0.products item=prod name=products}
 {$smarty.foreach.products.iteration}
{/foreach}


Another idea is to assign a variable and increment it yourself each time:
Code:

{assign var="increment" value=1}
{foreach from=$orders.0.products item=prod name=products}
  Increment: {$increment}
  {assign var="increment" value=$increment+1}
{/foreach}


MAPerformance Admin 03-13-2015 12:15 PM

Re: Incrementally number in a Smarty template
 
Thank you! I tried the second option and it appears to be working well.


All times are GMT -8. The time now is 01:23 AM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.