Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Usage  



1.1  Caution  





1.2  Substitution  







2 Examples  



2.1  Other examples with explicit values  







3 Notes about legacy code  





4 See also  














Template:For loop






Afrikaans
Аԥсшәа
العربية

Azərbaycanca
تۆرکجه
Basa Bali

 / Bân-lâm-gú
Башҡортса
Беларуская (тарашкевіца)
Bosanski
Буряад
Català
Cebuano
Corsu
Cymraeg
Deutsch
Dolnoserbski
Eesti
Ελληνικά
Español
Esperanto
فارسی
Français
Galego

Gungbe
/Hak-kâ-ngî

Հայերեն
ि
Hornjoserbsce
Ilokano
Bahasa Indonesia
Íslenska
Italiano
Jawa

Kurdî
Ladin
Latviešu
Lietuvių
Ligure
Magyar
Македонски

Bahasa Melayu
 / Mìng-dĕ̤ng-nḡ
Nederlands


Нохчийн
ି
Oʻzbekcha / ўзбекча
پښتو
Polski
Português
Română
Русиньскый
Русский
Саха тыла
Scots

Simple English
سنڌي
Slovenčina
Slovenščina
کوردی
Српски / srpski
Suomi
Tagalog
ி
 

Türkçe
Українська
اردو
Vèneto
Tiếng Vit
Walon

Zazaki

 

Edit links
 









Template
Talk
 

















Read
View source
View history
 








Tools
   


Actions  



Read
View source
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 




In other projects  



Wikimedia Commons
Meta-Wiki
Wikimedia Outreach
Wikiversity
 
















Appearance
   

 





Permanently protected template

From Wikipedia, the free encyclopedia
 


This template implements a for loop or a foreach loop.

This template calls a user-specified template (the "called template") multiple times: once for each value in either 1) an iterated sequence or 2) an explicit list. Each value in the sequence or list is passed to the same specified parameter of the called template (the "variable parameter"). Optionally, pre- and postfixes can be attached to the passed values.

In addition to the specified variable parameter, other parameters of the called template ("static parameters") can be given a value which is the same in each iteration.

Usage

Group Parameter Meaning Default value
Mandatory 1 (unnamed) separator to output between calls (may be blank; whitespace is preserved)
call template to call
pv name (or number) of the variable parameter of the called template 1
Option 1:
iterated sequence
start first numeric value to pass to variable parameter 1
stop maximum numeric value for variable parameter
by iteration step size between values passed to variable parameter 1
Option 2:
explicit values
(unnamed parameters) explicit values, given as separate parameters (whitespace is stripped)
skipBlanks set to "true" to skip empty parameter values false
Other optional
parameters
pc[N]n name (or number) of the Nth static parameter of the called template
pc[N]v value for the Nth static parameter of the called template
prefix static prefix prepended to each value passed to the variable parameter
postfix static postfix appended to each value passed to the variable parameter
substall set to "false" to not substitute the called template when {{for loop}} is substituted true

Either option 1 (iteration parameters) or option 2 (an explicit list of values) may be used, but not both.

The first unnamed parameter, prior to any explicit values, is a separator. The separator is a string that is output between calls to the template named in |call=. It is not output after the last call.

Caution

Substitution

The current Lua-based template supports substitution. If |substall=no is not specified, then substituting the template will substitute everything, including the call to the template passed in |call=. If it is specified, then the template substitutes into a sequence of calls to the template specified.

Example: {{subst:for loop|sep|01|02|03|04|05|06|07|call=1x}} -> 01sep 02sep 03sep 04sep 05sep 06sep 07, {{subst:for loop|sep|01|02|03|04|05|06|07|substall=no}} -> {{1x|01}}sep {{1x|02}}sep {{1x|03}}sep {{1x|04}}sep {{1x|05}}sep {{1x|06}}sep {{1x|07}}

For full substitution Special:ExpandTemplates can also be used.

Examples

Form Code Explanation Result
Iterator
{{for loop|&
 |call = spanbox
 |pv= font size
  |start=10|stop=52|by=8|postfix =px|pc1n =1|pc1v =A|pc2n = background
 |pc2v = yellow
}}
Call the template "spanbox" with values:
  • font size = "[N]px", where N takes values starting with 10 and increasing by 8 while remaining less than or equal to 52
  • 1 (first unnamed parameter) = "A"
  • background = "yellow"

Separating outputs with "&"

A

&

A

&

A

&

A

&

A

&

A

Explicit values
{{for loop|, |call=2x
|prefix=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 the template {{2x}} (which just repeats its input twice) with values "1[NN]", where NN = "00" through "99" (given explicitly), separating the outputs with the string ", " 100100, 101101, 102102, 103103, 104104, 105105, 106106, 107107, 108108, 109109, 110110, 111111, 112112, 113113, 114114, 115115, 116116, 117117, 118118, 119119, 120120, 121121, 122122, 123123, 124124, 125125, 126126, 127127, 128128, 129129, 130130, 131131, 132132, 133133, 134134, 135135, 136136, 137137, 138138, 139139, 140140, 141141, 142142, 143143, 144144, 145145, 146146, 147147, 148148, 149149, 150150, 151151, 152152, 153153, 154154, 155155, 156156, 157157, 158158, 159159, 160160, 161161, 162162, 163163, 164164, 165165, 166166, 167167, 168168, 169169, 170170, 171171, 172172, 173173, 174174, 175175, 176176, 177177, 178178, 179179, 180180, 181181, 182182, 183183, 184184, 185185, 186186, 187187, 188188, 189189, 190190, 191191, 192192, 193193, 194194, 195195, 196196, 197197, 198198, 199199

Other examples with explicit values

{{for loop|-|a|3||c|g|call=3x}}
using Template:3x (backlinks edit) 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}}
|}

gives:

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}}

gives:

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

gives:

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

Notes about legacy code

This template is currently in its third incarnation. It now uses the Lua code at Module:For loop. 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 Template:For on meta. 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.

See also


Retrieved from "https://en.wikipedia.org/w/index.php?title=Template:For_loop&oldid=851685775"

Categories: 
Lua-based templates
Wikipedia utility templates
Hidden category: 
Wikipedia template-protected templates
 



This page was last edited on 23 July 2018, at 22:55 (UTC).

Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



Privacy policy

About Wikipedia

Disclaimers

Contact Wikipedia

Code of Conduct

Developers

Statistics

Cookie statement

Mobile view



Wikimedia Foundation
Powered by MediaWiki