|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
THO Smarty n00b
Joined: 07 Jan 2022 Posts: 3
|
Posted: Fri Jan 07, 2022 1:45 pm Post subject: Creating if/counter/foreach based on number instead of array |
|
|
Hi everyone,
im looking for a way to create a counter/iteration with a repitition based on a number instead of an array. The result will be placed in an XML-File.
My shop will create something like:
$order.amount => x while x can be a number from 1 to n
If x is > 1 i want to have a repitition in my XML-output like this:
<ResourceSet Name="RunList">
<Resource>
<Part Run="Part-01" />
<RunList>
<FileSpec URL="file://wieapp00950/TestData/file_1.pdf" />
</RunList>
</Resource>
<Resource>
<Part Run="Part-02" />
<RunList>
<FileSpec URL="file://wieapp00950/TestData/file_2.pdf" />
</RunList>
</Resource>
</ResourceSet>
Is there a way to start an iteration or counter with max-number of $order.amount that i can use in a foreach-loop for the ResourceSet of my XML-file?
Thanks in advance |
|
Back to top |
|
THO Smarty n00b
Joined: 07 Jan 2022 Posts: 3
|
Posted: Mon Jan 10, 2022 9:45 am Post subject: |
|
|
I have found a way to get the result i was looking for. But i suppose there is a smarter way, using a counter for the first part, so the foreach doesn't have to go through all items checking if the value is <= $order.amount.
My approach for now is:
Code: | {assign var=count value=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]}
{foreach from=$count item=item key=key}
{if $item <= $order.amount}
<Resource>
<Part Run="Part-{$item}" />
<RunList>
<FileSpec URL="file://wieapp00950/TestData/file_{$item}.pdf" />
</RunList>
</Resource>
{/if}
{/foreach} |
|
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
|
Back to top |
|
SlowFox Smarty Regular
Joined: 02 Oct 2006 Posts: 55
|
Posted: Sat Jan 29, 2022 10:16 pm Post subject: |
|
|
Without having tested it, but why not
Code: |
{for $item=1 to $order.amount}
<Resource>
<Part Run="Part-{$item}" />
<RunList>
<FileSpec URL="file://wieapp00950/TestData/file_{$item}.pdf" />
</RunList>
</Resource>
{/for}
|
|
|
Back to top |
|
THO Smarty n00b
Joined: 07 Jan 2022 Posts: 3
|
Posted: Tue Feb 01, 2022 7:38 am Post subject: |
|
|
Works like a charm, thank you SlowFox! |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|