![]() |
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> |
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} Another idea is to assign a variable and increment it yourself each time: Code:
{assign var="increment" value=1} |
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.