rejetto forum

doforlist function :)

Roessi · 3 · 1157

0 Members and 1 Guest are viewing this topic.

Offline Roessi

  • Tireless poster
  • ****
    • Posts: 107
    • View Profile
this is for everyone this is useful for :)

Code: [Select]
{.set|doforlist|
{:
{.set|#seperator|{.switch|{.length|$3.}||0|;|$3.}.}
{.set|#list|$1{.^#seperator.}.}
{.set|#entries|{.count substring|{.^#seperator.}|{.^#list.}.}.}
{.for|ii|1|{.^#entries.}|
{:
{.set|#curitem|{.substring||{.^#seperator.}|{.^#list.}.}.}
{.set|#list|{.replace|{.^#curitem.}{.^#seperator.}||{.^#list.}.}.}
{.set|domacro|{.replace|%cur%|{.^#curitem.}|$2.}.}
{.^domacro.}
:}
.}
:}
.}

u can use it for example to show an admin page in an template with html for each user or sth. like this

Code: [Select]
{.^doforlist| list |{: command :}| optional: seperator .}
it executes the commad in parameter so many times like items exist in the list :) use %cur% for the current item f.e. :

Code: [Select]
{.^doforlist|{.get|accounts.}|{:%cur%:{.get account|%cur%|member of.}:}.}
would return every user and and the group hes member of

just thought it might be useful for someone :)


Offline rejetto

  • Administrator
  • Tireless poster
  • *****
    • Posts: 13308
    • View Profile
my contribution:
with the way you use to remove the current item from the list, a list like this
a;b;ba;a;c
would be treated like it was this
a;b;c

that's bad.
by using {.cut.} you solve this problem, and it's even faster.
i also used {.dequote.} to get the $2 executed, instead of using variable domacro.
i added {.trim.} to avoid adding extra spaces.

Code: [Select]
{.set|#doforlist|{:{.trim|
{.set|#seperator|{.switch|{.length|$3.}||0|;|$3.}.}
{.set|#list|$1{.^#seperator.}.}
{.set|#entries|{.count substring|{.^#seperator.}|{.^#list.}.}.}
{.while|#list|{:{.trim|
{.set|#curitem|{.substring||{.^#seperator.}|{.^#list.}.}.}
{.cut|var=#list|from={.add|2|{.length|var=#curitem.}.}|size=.}
.}{.dequote|{.replace|%cur%|{.^#curitem.}|$2.}.}:}.}
.}:}.}


EXAMPLE

{.^#doforlist|{.get|accounts.}|{:%cur%:{.get account|%cur%|member of.}
:}.}

i hope you enjoy this changes


Offline Roessi

  • Tireless poster
  • ****
    • Posts: 107
    • View Profile
1000 thx :) works perfectly :)
my honor to you for caring bout every single persons problems ;)
this is really nice of you ;)