Forum:Arbitrary template arguments

Let's say I have a template that takes an arbitrary number of arguments, numbered arg1 to argn, and does something with them and a related variable. Is there a "foreach" style operator or way to do something similar using template magic? The way I'm currently doing it is:

Which continues up to a number that I figure no one would surpass. If there's a way to consolidate this down to, say, "foreach_operator args dosomething", that would be nice. I saw a Foreach template on MediaWiki that looked intriguing, but I'm not sure if the way I want to use it is legal. Can I give it empty arguments, like say arg1 to arg30, where the empty arguments are just ignored? And can I curry it, turning it into:

Into something where SpecialTemplate extracts the argument number and does something with otherarg1, otherarg2, ..., otherargn? The hangup I'm having here is I'm not sure if that sort of currying is even possible. Will #replace work here, so we get a SpecialTemplate like:

fillertext,

Or does that not do what I think it does?

Anyone have any advice here, or is the way I did it where you just expand the ifs out for like 10 options the only way? Surgo 06:28, 13 August 2009 (UTC)


 * Included with the semantic forms extension are the functions #arraymap and #arraytemplate. These iterate on a string with a delimiter specified by the caller.  I have used this to pack parameters into a string, then evaluate them in an equivalent of what you refer to as a foreach loop.  There may be better ways.  That's what came to mind- it may or may not be useful.   21:57, 13 August 2009 (UTC)


 * Depending on the exact usage, it may be possible to do a recursive statement like this:


 * Still has the hard-coded limit on the number of arguments, but it is much neater than having all of the arguments copied and pasted into the same template. On the other hand, I believe (not thoroughly familiar with the Wiki system) that this would lead to massively more overhead on the Template's end, which would be bad for any commonly-used template.
 * DragoonWraith &dagger;  08:28, September 30, 2009 (UTC)

While it is not impossible to do recursion in wikitext, there are hurdles. One problem is that wikimedia engine is aggressive about terminating malformed templates and will fail the above code because of the detected loop. It is possible to flip between templates to do pseudo recursion, but it makes for some very difficult to read code. Unless you plan to stick around at a wiki forever, other contributors are not going to be able to maintain your template. 07:12, October 2, 2009 (UTC)

mw:Extension:Loops – koisuru ( talk ) 13:10, October 5, 2009 (UTC)