Plantilla:For loop

De la Viquipèdia, l'enciclopèdia lliure
Icona de documentació de mòdul Documentació de la plantilla [ mostra ] [ modifica el codi ] [ mostra l'historial ] [ refresca ]

Plantilla:For loop implementa un bucle foreach.

Aquesta plantilla crida a una plantilla especificada per l'usuari una vegada per a cada un dels valors d'una llista. Un paràmetre amb nom es passa amb el valor de la llista de la iteració actual amb un possible prefix i/o sufix adjunts. A més del paràmetre variable, es pot especificar pels paràmetres fixos de passar amb cada iteració. El primer paràmetre sense nom s'emet entre les crides a la plantilla especificada per l'usuari.

Ús[modifica]

{{for loop|<!-- separador, es conserven els espais en blanc -->|
    call    = plantilla a cridar
  | pc1n    = nom del primer paràmetre estàtic passat en la crida a la plantilla
  | pc1v    = valor del primer paràmetre estàtic passat en la crida a la plantilla
  | pc2n    = nom del 2n paràmetre estàtic passat en la crida a la plantilla
  | pc2v    = valor del 2n paràmetre estàtic passat en la crida a la plantilla
  | pc3n    = nom del 3r paràmetre estàtic passat en la crida a la plantilla
  | pc3v    = valor del 3r paràmetre estàtic passat en la crida a la plantilla
<!-- Més paràmetres fixos en la crida a la plantilla es poden inserir segons sigui necessari -->
  | pv      = nom del paràmetre usat per passar en la crida a la plantilla a la qual és
                assignat els valors de les variables amb prefix i postfix.
  | prefix  = prefix estàtic que s'anteposa a cadascun dels valors de les variables
  | postfix = prefix estàtic que es posposa a cadascun dels valors de les variables
  | llista de valors de les variables amb el separador "|", l'espai en blanc és stripped
     (els valors dels paràmetres buits treballen en la forma regular, ''no'' són ignorats com els indefinits)
}}

The separator is the first unnamed parameter, allowing it to start and/or end with newlines and spaces. The separator is output between calls to the template named in |call=. It is not output after the last call to that template. The separator parameter is prior to the value list. If a value in the list contains an equals sign, use {{=}} (see Help:Template). If you do not use {{=}}, the list must use named parameters, at least from that point. If you use named parameters, note that the first value is parameter 2 (e.g. |2=Your1stValue), because parameter 1 is the separator. If the separator contains an equals sign, {{=}} should also be used. Alternately, the separator can similarly be prefixed with "1=", but in that case it cannot contain newlines and spaces at the start and end. Also, if you use named parameters, you must not skip any numbers. The loop will terminate after the first absent numbered parameter. (Note that parameters can be blank but not absent; blank parameters are read as normal.)

Exemples[modifica]

Codi Resultat
1{{for loop|, 1|00|01|02|03|04|05|06|07|08|09
|10|11|12|13|14|15|16|17|18|19
|20|21|22|23|24|25|26|27|28|29
|30|31|32|33|34|35|36|37|38|39
|40|41|42|43|44|45|46|47|48|49
|50|51|52|53|54|55|56|57|58|59
|60|61|62|63|64|65|66|67|68|69
|70|71|72|73|74|75|76|77|78|79
|80|81|82|83|84|85|86|87|88|89
|90|91|92|93|94|95|96|97|98|99
  |call=1x
}}
100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199

Note that the last part of the separator is used as a prefix; the first prefix needs to be put separately.


{{for loop|-|a|3||c|g|call=3x}} using Plantilla:3x (què hi enllaça, edita) gives

aaa-333--ccc-ggg

{{for loop|

|a|3||c|g|call=3x}} gives

aaa

333


ccc

ggg

{|class="wikitable sortable"
|-
!Test
|-
| {{for loop|
{{!}}-
{{!}} |a|b|c|d|e|call=3x}}
|}

dóna:

Test
aaa
bbb
ccc
ddd
eee
{{for loop| |01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19
|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39
|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59
|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79
|80|81|82|83|84|85|86|87|88|89|90|91|92|93|94|95|96|97|98|99
|100|101|102|103|104|105|106|107|108|109|110|111|112|113|114|115|116|117|118|119
|120|121|122|123|124|125|126|127|128|129|130|131|132|133|134|135|136|137|138|139
|140|141|142|143|144|145|146|147|148|149|150|151|152|153|154|call=1x}}

dóna:

01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

begin->{{for loop|{{=}} |01|02|03|04|05|06|07|08|09|10
|11{{=}}{{=}}{{=}}|12|13|14|15|16|17|18|19|call=1x}}<-end

dóna:

begin->01= 02= 03= 04= 05= 06= 07= 08= 09= 10= 11==== 12= 13= 14= 15= 16= 17= 18= 19<-end

Codi llegat[modifica]

This template is currently in its third incarnation. It now uses the Lua code at Module:ForLoop. It was ported to Lua from ParserFunctions. There was also a previous version with a named "sep" parameter to specify the separator value. The template was originally based on m:Template:For. The template name was changed because there was already a Template:For on Wikipedia.

The old versions were limited to 150 variable values and four fixed parameters. There are no such limits in the current version. Also, in the first version the "sep" parameter didn't allow whitespace in the separator value. This was fixed with the second version and was retained in the current version.

Substitució[modifica]

The current Lua-based template supports substitution of the whole template call. To substitute everything, use {{subst:for loop}}.

For full substitution Special:ExpandTemplates can also be used.

Converting legacy template invocations[modifica]

If you need to convert a template invocation that was written for the original version of {{for loop}}, do the following:

  • replace pc1=X=Y with pc1n=X | pc1v=Y, etc.
  • use an unnamed parameter for the separator; if empty, just enter an extra "|"

No conversion is necessary for invocations written for the second version of the template, unless they use fine-grained substitution. If you need fine-grained substitution you should use Plantilla:Tlu instead of {{for loop}}. — Mr. Stradivarius ♪ talk ♪ 09:44, 19 July 2013 (UTC)

Vegeu també[modifica]