Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
MainPage
Community portal
Recent changes
Random page
Help
Special pages
Donate
Search
Search
English
Appearance
Log in
Request account
Personal tools
Log in
Request account
Export translations
Translate
English
Language statistics
Message group statistics
Export
Tools
Tools
move to sidebar
hide
Actions
Language statistics
Message group statistics
Export
General
Printable version
In other projects
Appearance
move to sidebar
hide
Settings
Group
Category:Maintenance
Help:Blocking users
Help:Bots
Help:Categories
Help:Contents
Help:Deletion and undeletion
Help:Diff
Help:Editing pages
Help:ExpandTemplates
Help:Extension:ParserFunctions
Help:External searches
Help:Formatting
Help:Images
Help:Links
Help:Magic words
Help:Managing files
Help:Moving a page
Help:Namespaces
Help:Navigation
Help:Parser functions in templates
Help:Patrolled edits
Help:Preferences
Help:Protected pages
Help:Random page
Help:Random root page
Help:Range blocks
Help:Recent changes
Help:Redirects
Help:Skins
Help:Special pages
Help:Starting a new page
Help:Substitution
Help:System message
Help:Tables
Help:Talk pages
Help:TemplateData
Help:Templates
Help:TemplateStyles
Help:Tracking changes
Help:Undelete
Help:User contributions
Help:Watchlist
Help:What links here
Manual:Expr parser function syntax
Manual:Extending wiki markup
Manual:Hooks
Manual:Magic words
Manual:Messages API
Manual:Parser functions
Module:Arguments/doc
Module:Int/doc
Module:Message box/configuration/doc
Module:Message box/doc
Module:Template translation/doc
Module:Transcluder/doc
Module:Version/doc
Module:Yesno/doc
News
Parser extension tags
Parser function extensions
Project:Language policy
Template:Admin tip
Template:Advanced user manual
Template:Anontools
Template:ApiEx
Template:Autocat
Template:Auxiliary template common notice
Template:Branching
Template:Bugfix
Template:Bugzilla
Template:Button
Template:Candidate
Template:Caution
Template:Clarify
Template:Collapse top
Template:Component
Template:Deprecated-inline
Template:Description missing
Template:Distinguish
Template:Documentation
Template:Documentation subpage
Template:Done
Template:EmptyCatGood
Template:Escape template list
Template:Excerpt
Template:Extension development
Template:Fixtext
Template:For
Template:Further
Template:Help
Template:High-risk
Template:Historical
Template:I18n navigation
Template:In progress
Template:Intricate template
Template:Introduced-inline
Template:IRC
Template:Localized link
Template:Localized link/messagedoc
Template:Magic words
Template:Main
Template:MainPage
Template:Markup
Template:Markup/row
Template:Mbox templates
Template:Module rating
Template:Move
Template:MW version
Template:MW version/layout
Template:N/a
Template:Navbar
Template:Newarticletext
Template:No
Template:Not done
Template:Note
Template:Optional
Template:Parser functions nav
Template:Partially done
Template:PD Help Page
Template:Phabricator
Template:Ptag
Template:Purge
Template:Recentchangestext
Template:Removed-inline
Template:Required
Template:Rev
Template:Security
Template:See
Template:See also
Template:Sp-contributions-footer
Template:Sp-contributions-footer-anon
Template:Special page
Template:Template shortcut
Template:Template test cases notice
Template:TemplateData header
Template:Thankyou
Template:To do
Template:Tracked
Template:Translatable
Template:Translatable template name
Template:Unusedtemplatestext
Template:Update
Template:Used in system
Template:Watchlists navigation
Template:Why
Template:WikiNonStop News
Template:Yes
TemplateData
Language
aa - Afar
aae - Arbëresh
ab - Abkhazian
abr - Abron
abs - Ambonese Malay
ace - Acehnese
acf - Saint Lucian Creole
acm - Iraqi Arabic
ady - Adyghe
ady-cyrl - Adyghe (Cyrillic script)
aeb - Tunisian Arabic
aeb-arab - Tunisian Arabic (Arabic script)
aeb-latn - Tunisian Arabic (Latin script)
af - Afrikaans
aig - Antiguan and Barbudan Creole English
aln - Gheg Albanian
alt - Southern Altai
am - Amharic
ami - Amis
an - Aragonese
ang - Old English
ann - Obolo
anp - Angika
apc - Levantine Arabic
ar - Arabic
arc - Aramaic
arn - Mapuche
arq - Algerian Arabic
ary - Moroccan Arabic
arz - Egyptian Arabic
as - Assamese
ase - American Sign Language
ast - Asturian
atj - Atikamekw
av - Avaric
avk - Kotava
awa - Awadhi
ay - Aymara
az - Azerbaijani
azb - South Azerbaijani
ba - Bashkir
ban - Balinese
ban-bali - Balinese (Balinese script)
bar - Bavarian
bbc - Batak Toba
bbc-latn - Batak Toba (Latin script)
bcc - Southern Balochi
bci - Baoulé
bcl - Central Bikol
bdr - West Coast Bajau
be - Belarusian
be-tarask - Belarusian (Taraškievica orthography)
bew - Betawi
bg - Bulgarian
bgc - Haryanvi
bgn - Western Balochi
bh - Bhojpuri
bho - Bhojpuri
bi - Bislama
bjn - Banjar
blk - Pa'O
bm - Bambara
bn - Bangla
bo - Tibetan
bpy - Bishnupriya
bqi - Bakhtiari
br - Breton
brh - Brahui
bs - Bosnian
btm - Batak Mandailing
bto - Rinconada Bikol
bug - Buginese
bug-bugi - Buginese (Buginese script)
bxr - Russia Buriat
ca - Catalan
cbk-zam - Chavacano
ccp - Chakma
cdo - Mindong
cdo-hant - Mindong (Traditional Han script)
cdo-latn - Mindong (Latin script)
ce - Chechen
ceb - Cebuano
ch - Chamorro
chn - Chinook Jargon
cho - Choctaw
chr - Cherokee
chy - Cheyenne
ckb - Central Kurdish
co - Corsican
cop - Coptic
cps - Capiznon
cpx - Puxian
cpx-hans - Puxian (Simplified Han script)
cpx-hant - Puxian (Traditional Han script)
cpx-latn - Puxian (Latin script)
cr - Cree
crh - Crimean Tatar
crh-cyrl - Crimean Tatar (Cyrillic script)
crh-latn - Crimean Tatar (Latin script)
crh-ro - Dobrujan Tatar
cs - Czech
csb - Kashubian
cu - Church Slavic
cv - Chuvash
cy - Welsh
da - Danish
dag - Dagbani
de - German
de-at - Austrian German
de-ch - Swiss High German
de-formal - German (formal address)
dga - Southern Dagaare
din - Dinka
diq - Dimli
dlg - Dolgan
dsb - Lower Sorbian
dtp - Central Dusun
dty - Doteli
dua - Duala
dv - Divehi
dz - Dzongkha
ee - Ewe
efi - Efik
egl - Emilian
el - Greek
eml - Emiliano-Romagnolo
en - English
en-ca - Canadian English
en-gb - British English
eo - Esperanto
es - Spanish
es-419 - Latin American Spanish
es-formal - Spanish (formal address)
et - Estonian
eu - Basque
ext - Extremaduran
fa - Persian
fat - Fanti
ff - Fula
fi - Finnish
fit - Tornedalen Finnish
fj - Fijian
fo - Faroese
fon - Fon
fr - French
frc - Cajun French
frp - Arpitan
frr - Northern Frisian
fur - Friulian
fvr - Fur
fy - Western Frisian
ga - Irish
gaa - Ga
gag - Gagauz
gan - Gan
gan-hans - Gan (Simplified Han script)
gan-hant - Gan (Traditional Han script)
gcf - Guadeloupean Creole
gcr - Guianan Creole
gd - Scottish Gaelic
gl - Galician
gld - Nanai
glk - Gilaki
gn - Guarani
gom - Goan Konkani
gom-deva - Goan Konkani (Devanagari script)
gom-latn - Goan Konkani (Latin script)
gor - Gorontalo
got - Gothic
gpe - Ghanaian Pidgin
grc - Ancient Greek
gsw - Alemannic
gu - Gujarati
guc - Wayuu
gur - Frafra
guw - Gun
gv - Manx
ha - Hausa
hak - Hakka Chinese
hak-hans - Hakka (Simplified Han script)
hak-hant - Hakka (Traditional Han script)
hak-latn - Hakka (Latin script)
haw - Hawaiian
he - Hebrew
hi - Hindi
hif - Fiji Hindi
hif-latn - Fiji Hindi (Latin script)
hil - Hiligaynon
hke - Hunde
hno - Northern Hindko
ho - Hiri Motu
hoc-latn - Ho (Latin script)
hr - Croatian
hrx - Hunsrik
hsb - Upper Sorbian
hsn - Xiang
ht - Haitian Creole
hu - Hungarian
hu-formal - Hungarian (formal address)
hy - Armenian
hyw - Western Armenian
hz - Herero
ia - Interlingua
iba - Iban
ibb - Ibibio
id - Indonesian
ie - Interlingue
ig - Igbo
igl - Igala
ii - Sichuan Yi
ik - Inupiaq
ike-cans - Eastern Canadian (Aboriginal syllabics)
ike-latn - Eastern Canadian (Latin script)
ilo - Iloko
inh - Ingush
io - Ido
is - Icelandic
isv-cyrl - Interslavic (Cyrillic script)
isv-latn - Interslavic (Latin script)
it - Italian
iu - Inuktitut
ja - Japanese
jam - Jamaican Creole English
jbo - Lojban
jut - Jutish
jv - Javanese
ka - Georgian
kaa - Kara-Kalpak
kab - Kabyle
kai - Karekare
kaj - Jju
kbd - Kabardian
kbd-cyrl - Kabardian (Cyrillic script)
kbp - Kabiye
kcg - Tyap
kea - Kabuverdianu
kg - Kongo
kge - Komering
khw - Khowar
ki - Kikuyu
kiu - Kirmanjki
kj - Kuanyama
kjh - Khakas
kjp - Eastern Pwo
kk - Kazakh
kk-arab - Kazakh (Arabic script)
kk-cn - Kazakh (China)
kk-cyrl - Kazakh (Cyrillic script)
kk-kz - Kazakh (Kazakhstan)
kk-latn - Kazakh (Latin script)
kk-tr - Kazakh (Turkey)
kl - Kalaallisut
km - Khmer
kn - Kannada
knc - Central Kanuri
ko - Korean
ko-kp - Korean (North Korea)
koi - Komi-Permyak
kr - Kanuri
krc - Karachay-Balkar
kri - Krio
krj - Kinaray-a
krl - Karelian
ks - Kashmiri
ks-arab - Kashmiri (Arabic script)
ks-deva - Kashmiri (Devanagari script)
ksh - Colognian
ksw - S'gaw Karen
ku - Kurdish
ku-arab - Kurdish (Arabic script)
ku-latn - Kurdish (Latin script)
kum - Kumyk
kus - Kusaal
kv - Komi
kw - Cornish
ky - Kyrgyz
la - Latin
lad - Ladino
lb - Luxembourgish
lbe - Lak
lez - Lezghian
lfn - Lingua Franca Nova
lg - Ganda
li - Limburgish
lij - Ligurian
liv - Livonian
ljp - Lampung Api
lki - Laki
lld - Ladin
lmo - Lombard
ln - Lingala
lo - Lao
loz - Lozi
lrc - Northern Luri
lt - Lithuanian
ltg - Latgalian
lua - Luba-Lulua
lus - Mizo
luz - Southern Luri
lv - Latvian
lzh - Literary Chinese
lzz - Laz
mad - Madurese
mag - Magahi
mai - Maithili
map-bms - Banyumasan
mdf - Moksha
mg - Malagasy
mh - Marshallese
mhr - Eastern Mari
mi - Māori
min - Minangkabau
mk - Macedonian
ml - Malayalam
mn - Mongolian
mnc - Manchu
mnc-latn - Manchu (Latin script)
mnc-mong - Manchu (Mongolian script)
mni - Manipuri
mnw - Mon
mo - Moldovan
mos - Mossi
mr - Marathi
mrh - Mara
mrj - Western Mari
ms - Malay
ms-arab - Malay (Jawi script)
mt - Maltese
mui - Musi
mus - Muscogee
mwl - Mirandese
my - Burmese
myv - Erzya
mzn - Mazanderani
na - Nauru
nah - Nahuatl
nan - Minnan
nan-hant - Minnan (Traditional Han script)
nan-latn-pehoeji - Minnan (Pe̍h-ōe-jī)
nan-latn-tailo - Minnan (Tâi-lô)
nap - Neapolitan
nb - Norwegian Bokmål
nds - Low German
nds-nl - Low Saxon
ne - Nepali
new - Newari
ng - Ndonga
nia - Nias
nit - Southeastern Kolami
niu - Niuean
nl - Dutch
nl-informal - Dutch (informal address)
nmz - Nawdm
nn - Norwegian Nynorsk
no - Norwegian
nod - Northern Thai
nog - Nogai
nov - Novial
nqo - N’Ko
nr - South Ndebele
nrm - Norman
nso - Northern Sotho
nup - Nupe
nv - Navajo
ny - Nyanja
nyn - Nyankole
nyo - Nyoro
nys - Nyungar
oc - Occitan
ojb - Northwestern Ojibwa
olo - Livvi-Karelian
om - Oromo
or - Odia
os - Ossetic
pa - Punjabi
pag - Pangasinan
pam - Pampanga
pap - Papiamento
pap-aw - Papiamento (Aruba)
pcd - Picard
pcm - Nigerian Pidgin
pdc - Pennsylvania German
pdt - Plautdietsch
pfl - Palatine German
pi - Pali
pih - Pitcairn-Norfolk
pl - Polish
pms - Piedmontese
pnb - Western Punjabi
pnt - Pontic
prg - Prussian
ps - Pashto
pt - Portuguese
pt-br - Brazilian Portuguese
pwn - Paiwan
qqq - Message documentation
qu - Quechua
qug - Chimborazo Highland Quichua
rgn - Romagnol
rif - Riffian
rki - Arakanese
rm - Romansh
rmc - Carpathian Romani
rmy - Vlax Romani
rn - Rundi
ro - Romanian
roa-tara - Tarantino
rsk - Pannonian Rusyn
ru - Russian
rue - Rusyn
rup - Aromanian
ruq - Megleno-Romanian
ruq-cyrl - Megleno-Romanian (Cyrillic script)
ruq-latn - Megleno-Romanian (Latin script)
rut - Rutul
rw - Kinyarwanda
ryu - Okinawan
sa - Sanskrit
sah - Yakut
sas - Sasak
sat - Santali
sc - Sardinian
scn - Sicilian
sco - Scots
sd - Sindhi
sdc - Sassarese Sardinian
sdh - Southern Kurdish
se - Northern Sami
se-fi - Northern Sami (Finland)
se-no - Northern Sami (Norway)
se-se - Northern Sami (Sweden)
sei - Seri
ses - Koyraboro Senni
sg - Sango
sgs - Samogitian
sh - Serbo-Croatian
sh-cyrl - Serbo-Croatian (Cyrillic script)
sh-latn - Serbo-Croatian (Latin script)
shi - Tachelhit
shi-latn - Tachelhit (Latin script)
shi-tfng - Tachelhit (Tifinagh script)
shn - Shan
shy - Shawiya
shy-latn - Shawiya (Latin script)
si - Sinhala
simple - Simple English
sjd - Kildin Sami
sje - Pite Sami
sk - Slovak
skr - Saraiki
skr-arab - Saraiki (Arabic script)
sl - Slovenian
sli - Lower Silesian
sm - Samoan
sma - Southern Sami
smn - Inari Sami
sms - Skolt Sami
sn - Shona
so - Somali
sq - Albanian
sr - Serbian
sr-ec - Serbian (Cyrillic script)
sr-el - Serbian (Latin script)
srn - Sranan Tongo
sro - Campidanese Sardinian
ss - Swati
st - Southern Sotho
stq - Saterland Frisian
sty - Siberian Tatar
su - Sundanese
sv - Swedish
sw - Swahili
syl - Sylheti
szl - Silesian
szy - Sakizaya
ta - Tamil
tay - Atayal
tcy - Tulu
tdd - Tai Nuea
te - Telugu
tet - Tetum
tg - Tajik
tg-cyrl - Tajik (Cyrillic script)
tg-latn - Tajik (Latin script)
th - Thai
ti - Tigrinya
tig - Tigre
tk - Turkmen
tl - Tagalog
tly - Talysh
tly-cyrl - Talysh (Cyrillic script)
tn - Tswana
to - Tongan
tok - Toki Pona
tpi - Tok Pisin
tr - Turkish
tru - Turoyo
trv - Taroko
ts - Tsonga
tt - Tatar
tt-cyrl - Tatar (Cyrillic script)
tt-latn - Tatar (Latin script)
ttj - Tooro
tum - Tumbuka
tw - Twi
ty - Tahitian
tyv - Tuvinian
tzm - Central Atlas Tamazight
udm - Udmurt
ug - Uyghur
ug-arab - Uyghur (Arabic script)
ug-latn - Uyghur (Latin script)
uk - Ukrainian
ur - Urdu
uz - Uzbek
uz-cyrl - Uzbek (Cyrillic script)
uz-latn - Uzbek (Latin script)
ve - Venda
vec - Venetian
vep - Veps
vi - Vietnamese
vls - West Flemish
vmf - Main-Franconian
vmw - Makhuwa
vo - Volapük
vot - Votic
vro - Võro
wa - Walloon
wal - Wolaytta
war - Waray
wls - Wallisian
wlx - Wali
wo - Wolof
wuu - Wu
wuu-hans - Wu (Simplified Han script)
wuu-hant - Wu (Traditional Han script)
xal - Kalmyk
xh - Xhosa
xmf - Mingrelian
xsy - Saisiyat
yi - Yiddish
yo - Yoruba
yrl - Nheengatu
yua - Yucatec Maya
yue - Cantonese
yue-hans - Cantonese (Simplified Han script)
yue-hant - Cantonese (Traditional Han script)
za - Zhuang
zea - Zeelandic
zgh - Standard Moroccan Tamazight
zgh-latn - Standard Moroccan Tamazight (Latin script)
zh - Chinese
zh-cn - Chinese (China)
zh-hans - Simplified Chinese
zh-hant - Traditional Chinese
zh-hk - Chinese (Hong Kong)
zh-mo - Chinese (Macau)
zh-my - Chinese (Malaysia)
zh-sg - Chinese (Singapore)
zh-tw - Chinese (Taiwan)
zu - Zulu
Format
Export for off-line translation
Export in native format
Export in CSV format
Fetch
<languages/> {{PD Help Page|imported=1}} <div lang="en" dir="ltr" class="mw-content-ltr"> '''Substitution''' is [[Special:MyLanguage/Help:Pre-save transform|automatic conversion of wikitext]] of a page when the page is saved, in the case that the wikitext refers to one or more [[Special:MyLanguage/Help:Templates|templates]], [[Special:MyLanguage/Help:variable|variables]], or [[Special:MyLanguage/Help:parser function|parser functions]]. </div> <span lang="en" dir="ltr" class="mw-content-ltr">In the case of template substitution the template call is replaced by the template content with substitution of the parameters.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Thus a template is used as [[wikt:en:macro|macro]] and the page is [[wikt:en:macro expansion|macro expanded]] when the page is saved rather than, as usually happens, when the page is viewed.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> In the case of substitution of a variable or parser function the reference to it is replaced by the resulting value. </div> <span lang="en" dir="ltr" class="mw-content-ltr">Substitution is done by putting the [[Special:MyLanguage/Help:Magic Words|magic word]] <code>subst:</code> or <code>safesubst:</code> after the double opening braces without intervening spaces like in the examples: <code><nowiki>{{subst:FULLPAGENAME}}</nowiki></code> and <code><nowiki>{{safesubst:FULLPAGENAME}}</nowiki></code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">The code <code>safesubst:</code> is useful in multilevel substitution, see [[#Multilevel substitution|below]].</span> <span lang="en" dir="ltr" class="mw-content-ltr">The result (in the form of the difference with the saved wikitext) can be seen before (or without) saving by pressing <code>{{int|showdiff}}</code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">However, if the text covers more than one paragraph this diff page is not very suitable for copying the result (e.g. for stepwise substitution without saving every step), because of plus signs in the margin.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> ==Applications== </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Substitution of a template: </div> * <span lang="en" dir="ltr" class="mw-content-ltr">Make a [[w:render|render]]ed page independent of the template:</span> ** <span lang="en" dir="ltr" class="mw-content-ltr">The rendered page does not change when the template is edited.</span> ** <span lang="en" dir="ltr" class="mw-content-ltr">The page can be copied to another MediaWiki wiki without copying the template.</span> * <span lang="en" dir="ltr" class="mw-content-ltr">Make page rendering easier and therefore faster for the server.</span> * <span lang="en" dir="ltr" class="mw-content-ltr">Analyse and demonstrate the working of templates.</span> <span lang="en" dir="ltr" class="mw-content-ltr">However, in some cases substitution works differently.</span> * <span lang="en" dir="ltr" class="mw-content-ltr">Make the correspondence between wikitext and rendered page easier to understand (this may apply, the opposite may also apply).</span> <div lang="en" dir="ltr" class="mw-content-ltr"> Substitution of a time-dependent variable: </div> * <span lang="en" dir="ltr" class="mw-content-ltr">Make a rendered page independent of the time.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> Substitution of a page-dependent variable: </div> * <span lang="en" dir="ltr" class="mw-content-ltr">Make a rendered page independent of renaming of the page and of copying the wikitext to another page (the opposite applies if the variable PAGENAME is used in a no-include part of the page to include the page itself).</span> <div lang="en" dir="ltr" class="mw-content-ltr"> Some MediaWiki extensions have the restriction that if they are used in a template with parameters, they only work if the template is substituted. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> ==Overview== </div> <span lang="en" dir="ltr" class="mw-content-ltr">For the discussion of '''substitution''', an ''"ordinary template"'' is the page that is referred to by either in the pattern {{tmpl|0={{((}}subst:$1<small> </small>{{))}}|''pagename''}} (for pages in the template namespace) or {{tmpl|0={{((}}subst:$1<small> </small>{{))}}|''fullpagename''}} (for pages in other namespaces).</span> <span lang="en" dir="ltr" class="mw-content-ltr">A ''"predefined template"'' is a [[Special:MyLanguage/Help:variable|variable]] or [[Special:MyLanguage/Help:parser function|parser function]] that is similarly substituted.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> Substitution is a separate process that is performed before expansion of any non-substituted templates, variables, parser functions, or parameters. </div> <span lang="en" dir="ltr" class="mw-content-ltr">A substitution call can have other substitution calls embedded in it.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Similarly a substitution's substituted template can contain other substitution calls.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Substitutions in the expression for the name of the template or parser function, in the parameter definitions of the substituted template or parser function and in the body of the substituted template are done first.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Since expansion is done later, any expression used in a substitution that happens to contain pairs of double braces, will have the braces treated as plain text.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Thus during substitution there can be a parameter name with braces in the substitution call (e.g. <code><nowiki>{{subst:foo|a{{bc}}d=...}}</nowiki></code>) that matches a parameter with the same name in the template body (e.g. <code><nowiki>{{{a{{bc}}d}}}</nowiki></code>).</span> <div lang="en" dir="ltr" class="mw-content-ltr"> If one attempts to apply substitution to a non-existing template etc. there is no substitution, the "subst:" prefix is kept in the wikitext. </div> <span lang="en" dir="ltr" class="mw-content-ltr">After the substitution process, expansion of templates etc. and other processing of the resulting wikitext works as usual.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Because substitution is over, this cannot undo a mismatch of parameter names that occurred during substitution (see also the section "[[#Partial substitution|Partial substitution]]" below).</span> <span lang="en" dir="ltr" class="mw-content-ltr">Substitution is only possible if the following has been fully evaluated:</span> *<span lang="en" dir="ltr" class="mw-content-ltr">the name of the template, variable, or parser function</span> *<span lang="en" dir="ltr" class="mw-content-ltr">in the case of a template: the parameter names in the template call and in the template itself</span> *<span lang="en" dir="ltr" class="mw-content-ltr">in the case of #if, #ifexpr, #ifexist and #iferror, the parameter after the colon</span> *<span lang="en" dir="ltr" class="mw-content-ltr">in the case of #ifeq, the parameter after the colon and the next one</span> *<span lang="en" dir="ltr" class="mw-content-ltr">in the case of #switch, the parameter after the colon and the expressions to the left of the equals signs</span> <div lang="en" dir="ltr" class="mw-content-ltr"> Since, as said, substitution is done before other expansion, the abovementioned required evaluation has not taken place if the expressions involve non-substituted expansion. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Also in the case of other parser functions than mentioned, a not fully evaluated parameter after the colon causes the parser function to be applied to the wikitext with braces and not the expanded wikitext, affecting the result. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Examples: </div> *<span lang="en" dir="ltr" class="mw-content-ltr">{{subst:Help:L<nowiki>{{</nowiki>tc}}k}} using [[Template:tc]], does not do substitution, because Help:L<nowiki>{{</nowiki>tc}}k is not an existing page, although <nowiki>Help:L{{tc}}k</nowiki> is rendered as Help:L{{tc}}k.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Thus the resulting wikitext is the same as the original wikitext and rendered as {{subst:Help:L{{tc}}k}}.</span> *<span lang="en" dir="ltr" class="mw-content-ltr"><nowiki>{{#if:{{void|abc}}|yes|no}}</nowiki> (using [[Template:void]]) is rendered as "{{#if:{{void|abc}}|yes|no}}", and so <nowiki>{{subst:#if:{{subst:void|abc}}|yes|no}}</nowiki> similarly gives the wikitext "no".</span> <span lang="en" dir="ltr" class="mw-content-ltr">On the other hand, <nowiki>{{subst:#if:{{void|abc}}|yes|no}}</nowiki> gives the wikitext "yes", because [[Template:void]] is not resolved until after substitution.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> In principle the wikitext resulting from full substitution is, immediately after that, rendered the same as the wikitext with ordinary inclusion. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Note however that on substitution of a parser function, an undefined parameter with default, used in a parameter value, is not replaced by the [[w:Help:Parameter default|default]] (in English Wikipedia), and is for example not allowed in the numerical expression evaluated on substitution of #expr: </div> :*<span lang="en" dir="ltr" class="mw-content-ltr"><nowiki>{{#expr:2*{{{p|3}}}}}</nowiki> gives {{#expr:2*{{{p|3}}}}}, while <nowiki>{{subst:#expr:2*{{{p|3}}}}}</nowiki> gives:</span> <strong class="error">Expression error: unrecognised punctuation character "{"</strong> <span lang="en" dir="ltr" class="mw-content-ltr">On substitution of a template that contains this, <nowiki>{{{p|3}}}</nowiki> is either replaced by the value of {{{p}}} or by 3, so then there is no complication.</span> <span lang="en" dir="ltr" class="mw-content-ltr"><nowiki>{{ {{t6}} }}</nowiki> using [[Template:t6]] containing "<code>{{msgnw:t6}}</code>" is rendered as {{ {{t6}} }}.</span> <span lang="en" dir="ltr" class="mw-content-ltr"><nowiki>{{subst:{{subst:t6}} }}</nowiki> gives the wikitext <nowiki>{{subst:t2demo|a }}</nowiki> rendered the same as the wikitext, and on the next edit/save changed into <!--t2-->start-a -middle-{{{2}}}-end.</span> <span lang="en" dir="ltr" class="mw-content-ltr"><nowiki>{{ {{subst:t6}} }}</nowiki> gives the wikitext <nowiki>{{ t2demo|a }}</nowiki>, rendered as {{ t2demo|a }}.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This is because, both without substitution and in the case of full substitution, the pipe characters in template calls, excluding those inside inner template calls, template parameters, links, and image tags, determine the separation of parameter definitions from each other and from the template name.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This separation does not depend on possible extra pipe characters in the expanded form of the template name and parameter definitions.</span> <span lang="en" dir="ltr" class="mw-content-ltr">However, if after substitution of an inner template the pipe character is in the outer template call it is one like any other and plays its part in determining the separation.</span> <span lang="en" dir="ltr" class="mw-content-ltr">In other words, parsing is done first once for substitution, and then once for rendering, but in both cases not an extra time in between.</span> <span lang="en" dir="ltr" class="mw-content-ltr">In the case of substitution of the inner template only, two subsequent parsings are effective.</span> <span lang="en" dir="ltr" class="mw-content-ltr">When substituting a template containing <nowiki>{{{p|q}}}</nowiki> (a parameter tag with default) this results in the value of p if it is defined, and otherwise in q.</span> <span lang="en" dir="ltr" class="mw-content-ltr">For example, using [[w:Template:Timc|{{timc|t pd}}]] (in English Wikipedia), <nowiki>{{subst:t pd}}</nowiki> gives the wikitext 2.</span> <span lang="en" dir="ltr" class="mw-content-ltr">If a page substitutes itself (e.g. in the noinclude-part of a template page) it substitutes the old version.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> ==Usage considerations== </div> <span lang="en" dir="ltr" class="mw-content-ltr">As mentioned, a change of an ordinary template after substitution does not affect the page in which it was substituted, and a substituted variable depending on time no longer depends on time, etc.</span> <span lang="en" dir="ltr" class="mw-content-ltr">However, a substitution of e.g. <nowiki>{{#expr:2*3}}</nowiki> does not affect rendering at all.</span> <span lang="en" dir="ltr" class="mw-content-ltr">The relationship between wikitext of a page and its rendering can become easier to understand after substitution, because one has all wikitext together, and parameter substitutions have been performed.</span> <span lang="en" dir="ltr" class="mw-content-ltr">It can also become more complex.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Separately focusing on understanding a template call and understanding the template content can be easier.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Wikitext after substitution is often more complex than when the required wikitext would have been written directly.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Unlike a template call (if one knows about templates), wikitext after substitution does not show how one can produce a similar result.</span> <span lang="en" dir="ltr" class="mw-content-ltr">The wikitext can be long and complicated, and therefore cumbersome to ''write'' directly, or it can be simple, e.g. a number resulting from a computation, but cumbersome to ''find'' directly.</span> <span lang="en" dir="ltr" class="mw-content-ltr">When studying the wikitext of a page one may think that this wikitext is what one is supposed to write and find directly to get the result, even in cases where that would be very impractical.</span> <span lang="en" dir="ltr" class="mw-content-ltr">In such cases documentation of the template call is useful.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Just like in computer programming we change the [[w:source code|source code]] and/or the data to produce new results, and we do not directly change the [[w:object file|object file]], here we would change the template calls and/or the templates, instead of changing the wikitext resulting from substitution directly.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> ===Ordinary templates=== </div> <div lang="en" dir="ltr" class="mw-content-ltr"> In the case of substituting an ordinary template, the template tag is replaced by the wikitext of the template, with the parameter values substituted for the parameters. </div> ;<span lang="en" dir="ltr" class="mw-content-ltr">Example:</span> <span lang="en" dir="ltr" class="mw-content-ltr">{{tim|t2}}, containing</span> ::<code><nowiki>start-{{{1}}}-middle-{{{2}}}-end</nowiki></code> : <span lang="en" dir="ltr" class="mw-content-ltr">and called as <code><nowiki>{{subst:t2|[[a]]|{{tc}}}}</nowiki></code> (see {{tl|tc}}) gives the wikitext:</span> ::<span lang="en" dir="ltr" class="mw-content-ltr"><code><nowiki>start-[[a]]-middle-{{tc}}-end</nowiki></code>, rendering as</span> ::start-[[a]]-middle-{{tc}}-end. <span lang="en" dir="ltr" class="mw-content-ltr">Substitution removes the noinclude parts and the includeonly tags.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Parameters:</span> *<span lang="en" dir="ltr" class="mw-content-ltr">A substitution with p=r replaces {{{p}}} and <nowiki>{{{p|q}}}</nowiki> by <code>r</code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This includes the cases that <code>r</code> is of the form {{{s}}} or <nowiki>{{{s|t}}}</nowiki>.</span> *<span lang="en" dir="ltr" class="mw-content-ltr">A substitution with undefined <code>p</code> preserves <code>{{{p}}}</code> and replaces <nowiki>{{{p|q}}}</nowiki> by the [[w:Help:Parameter default|default]] <code>q</code> (in English Wikipedia).</span> <span lang="en" dir="ltr" class="mw-content-ltr">With <code>subst:</code> the replacement of a template tag by wikitext does not work recursively.</span> <span lang="en" dir="ltr" class="mw-content-ltr">For full recursive substitution use [[Special:ExpandTemplates]].</span> <span lang="en" dir="ltr" class="mw-content-ltr">See also [[mediazilla:2777|substall]], and [[#Multilevel substitution|multilevel substitution]] below.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> Stepwise substitution of templates including other templates including more templates etc. can be useful for analyzing and documenting the behaviour of complex templates. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> However, after a substitution a page may render differently, e.g. if templates produce braces, pipes, and/or equals signs which after substitution determine expansion of other templates, but without substitution are treated as plain text. </div> <span lang="en" dir="ltr" class="mw-content-ltr">In the absence of parameters, template substitution can be compared with copying the wikitext, or the rendering of a previewed or saved {{tmpl|0={{((}}<small> </small><code>[[Special:MyLanguage/Help:Magic words|msgnw:]]</code>$1<small> </small>{{))}}|''pagename''}} inclusion.</span> <span lang="en" dir="ltr" class="mw-content-ltr">However, template substitution excludes {{tag|noinclude|open}} parts, removes {{tag|includeonly|open}} tags, and replaces undefined parameters with defaults by those defaults.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> ===Predefined templates=== </div> <div lang="en" dir="ltr" class="mw-content-ltr"> In the case of substituting a predefined template, without parameters depending on other templates, the tag is replaced by the result. </div> {{note|1=<span lang="en" dir="ltr" class="mw-content-ltr"><code>subst:</code> has to be added directly in front of the predefined template name '''without intervening spaces.'''</span>}} <span lang="en" dir="ltr" class="mw-content-ltr">Applying subst to a variable works like applying it to a template.</span> <span lang="en" dir="ltr" class="mw-content-ltr">E.g. a timestamp:</span> ;<nowiki>{{subst:CURRENTYEAR}}-{{subst:CURRENTMONTH}}-{{subst:CURRENTDAY}} T {{subst:CURRENTTIME}} [[w:UTC|]]</nowiki>: <span lang="en" dir="ltr" class="mw-content-ltr">may give the wikitext</span> :<nowiki>2010-04-10 T 06:30 [[w:UTC|UTC]]</nowiki> <span lang="en" dir="ltr" class="mw-content-ltr">rendered as</span> :2010-04-10 T 06:30 [[w:UTC|UTC]] <span lang="en" dir="ltr" class="mw-content-ltr">In the case of substituting a predefined template with a parameter depending on another template, that has to be substituted too, with a separate <code>subst:</code> modifier, otherwise the result is undefined.</span> *<code><nowiki>{{subst:UC:{{subst:tc}}}}</nowiki></code> – <span lang="en" dir="ltr" class="mw-content-ltr">gives IN, the same wikitext as <nowiki>{{UC:{{tc}}}}</nowiki> is expanded to; <code>UC:</code> is applied to the output "'''in'''" of [[m:Template:Tc|Tc]].</span> *<code><nowiki>{{subst:ns:{{subst:#expr:2*3}}}}</nowiki></code> – <span lang="en" dir="ltr" class="mw-content-ltr">gives File.</span> *<code><nowiki>{{ns:{{subst:#expr:2*3}}}}</nowiki></code> – <span lang="en" dir="ltr" class="mw-content-ltr">gives wikitext <nowiki>{{ns:6}}</nowiki> rendered as {{ns:6}}.</span> *<code><nowiki>{{subst:t1|{{subst:NAMESPACE}}}}</nowiki></code> – <span lang="en" dir="ltr" class="mw-content-ltr">gives the wikitext startHelpend (see {{tl|t1}})</span> *<code><nowiki>{{subst:t1|{{subst:#expr:3*4}}}}</nowiki></code> – <span lang="en" dir="ltr" class="mw-content-ltr">gives the wikitext start12end</span> *<code><nowiki>{{subst:t1|{{subst:uc:AbCdEf}}}}</nowiki></code> – <span lang="en" dir="ltr" class="mw-content-ltr">gives the wikitext startABCDEFend</span> *<code><nowiki>{{subst:#expr:{{subst:3X|11*}}1}}</nowiki></code> – <span lang="en" dir="ltr" class="mw-content-ltr">gives the wikitext {{#expr:{{3X|11*}}1}}</span> *<code><nowiki>{{subst:UC:{{subst:3X|abc}}}}</nowiki></code> – <span lang="en" dir="ltr" class="mw-content-ltr">gives the wikitext {{UC:{{3X|abc}}}}</span> *<code><nowiki>{{subst:LC:{{subst:#expr:1/100000}}}}</nowiki></code> – <span lang="en" dir="ltr" class="mw-content-ltr">gives the wikitext 1e-05</span> *<code><nowiki>{{subst:#expr:2*{{subst:CURRENTDAY}}}}</nowiki></code> – <span lang="en" dir="ltr" class="mw-content-ltr">gives (at the time of writing) the wikitext 30</span> *<code><nowiki>{{subst:UC:{{subst:CURRENTDAYNAME}}}}</nowiki></code> – <span lang="en" dir="ltr" class="mw-content-ltr">gives (at the time of writing) the wikitext THURSDAY</span> <span lang="en" dir="ltr" class="mw-content-ltr">However:</span> #<code><nowiki>{{subst:UC:{{tc}}}}</nowiki></code> – <span lang="en" dir="ltr" class="mw-content-ltr">gives the wikitext <code><nowiki>{{TC}}</nowiki></code> rendered as {{TC}}.</span> #<code><nowiki>{{subst:ns:{{#expr:2*3}}}}</nowiki></code> – <span lang="en" dir="ltr" class="mw-content-ltr">stays <nowiki>{{subst:ns:{{#expr:2*3}}}}</nowiki>, rendered as {{subst:ns:{{#expr:2*3}}}}.</span> <span lang="en" dir="ltr" class="mw-content-ltr">As mentioned before, on substitution, all calls without substitution of templates, variables, and parser functions are treated as plain text.</span> <span lang="en" dir="ltr" class="mw-content-ltr">As a result substitution of the outer <code>x:</code> in a nested {{<small> </small><code>x:'''...'''</code>{{<small> </small><code>y:'''...'''</code>}}<small> </small>}} often is only suitable if all inner <code>y:</code> are also substituted.</span> <span lang="en" dir="ltr" class="mw-content-ltr">In the case of substitution of a predefined template, if the expression for one of its parameters contains <nowiki>{{{p|3}}}</nowiki> with undefined <code>p</code>, this code reduces to 3.</span> <span lang="en" dir="ltr" class="mw-content-ltr">However, on the page itself, <nowiki>{{{p|3}}}</nowiki> is treated as such, not as 3.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Examples:</span> *<code><nowiki>{{#expr:2*{{{p}}}}}</nowiki></code> → <code>{{#expr:2*{{{p}}}}}</code> *<code><nowiki>{{#expr:2*{{{p|3}}}}}</nowiki></code> → <code>{{#expr:2*{{{p|3}}}}}</code> *<code><nowiki>{{subst:#expr:2*{{{p|3}}}}}</nowiki></code> → <strong class="error">Expression error: Unrecognized punctuation character "{".</strong> *<span lang="en" dir="ltr" class="mw-content-ltr">substituting a template containing <nowiki>{{<includeonly>subst:</includeonly>#expr:2*{{{p|3}}}}}</nowiki> gives 6 if <code>p</code> is not assigned a value, and twice the number <code>p</code> if it ''is'' assigned a value.</span> * <span lang="en" dir="ltr" class="mw-content-ltr">the same result can be obtained from a template containing <nowiki>{{{{{subst}}}#expr:2*{{{p|3}}}}}</nowiki>, if the substitution call has a parameter of "<code>subst=subst:</code>".</span> <span lang="en" dir="ltr" class="mw-content-ltr">Compare:</span> *<code><nowiki>{{uc:2*{{{p}}}}}</nowiki></code> → <code>{{uc:2*{{{p}}}}}</code> *<code><nowiki>{{uc:2*{{{p|q}}}}}</nowiki></code> → <code>{{uc:2*{{{p|q}}}}}</code> *<code><nowiki>{{subst:uc:2*{{{p|q}}}}}</nowiki></code> → <span lang="en" dir="ltr" class="mw-content-ltr">the wikitext <nowiki>2*{{{P|Q}}}</nowiki> rendered as 2*{{{P|Q}}}</span> <span lang="en" dir="ltr" class="mw-content-ltr">and also (from above):</span> *<code><nowiki>{{subst:UC:{{subst:tc}}}}</nowiki></code> – <span lang="en" dir="ltr" class="mw-content-ltr">gives IN, just like <nowiki>{{UC:{{tc}}}}</nowiki> does; {{ll|Help:Parser function|UC}} is applied to the output "'''in'''" of [[m:Template:Tc|Tc]].</span> *<code><nowiki>{{subst:UC:{{tc}}}}</nowiki></code> – <span lang="en" dir="ltr" class="mw-content-ltr">gives the wikitext <code><nowiki>{{TC}}</nowiki></code> rendered as {{TC}}.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> In the substitution of <code>UC</code>, the inclusion tag <nowiki>{{tc}}</nowiki> is treated as string just like <nowiki>{{{p|q}}}</nowiki>. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> ==Partial substitution== </div> <span lang="en" dir="ltr" class="mw-content-ltr">Inside an ordinary template one can apply substitution to an ordinary template call containing a parameter, to replace it by the direct wikitext containing the parameter.</span> <span lang="en" dir="ltr" class="mw-content-ltr">It amounts to automatically merging the two templates (creating a "composite template" like a [[w:Function composition|composite function]]).</span> <span lang="en" dir="ltr" class="mw-content-ltr">It is not possible if the inner and/or outer template is predefined.</span> <span lang="en" dir="ltr" class="mw-content-ltr">(However, manually merging e.g. a call of #expr inside another one is useful for increasing the accuracy of the result by avoiding intermediate rounding to 12 digits.)</span> <div lang="en" dir="ltr" class="mw-content-ltr"> This way one can dispense with the optional substitution technique described below, and apply substitution of the resulting outer template by simply using <code>subst:</code> (unless there are more nesting levels). </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Example: </div> *<code><nowiki>{{subst:t}}</nowiki></code> – <span lang="en" dir="ltr" class="mw-content-ltr">gives the wikitext <code><nowiki>start-{{{1|pqr}}}-end</nowiki></code>, just that of {{tim|t}}, without noinclude parts and includeonly tags</span> *<code><nowiki>{{subst:t|a{{{p|q}}}b}}</nowiki></code> – <span lang="en" dir="ltr" class="mw-content-ltr">gives the wikitext <code><nowiki>start-a{{{p|q}}}b-end</nowiki></code></span> <span lang="en" dir="ltr" class="mw-content-ltr">Examples with double substitution:</span> *<code><nowiki>{{subst:3X|{{subst:t}}}}</nowiki></code> – <span lang="en" dir="ltr" class="mw-content-ltr">gives the wikitext <code><nowiki>start-{{{1|pqr}}}-endstart-{{{1|pqr}}}-endstart-{{{1|pqr}}}-endstart-{{{1|q}}}-end</nowiki></code></span> *<code><nowiki>{{subst:3X|{{subst:t|{{{1|q}}}}}}}</nowiki></code> – <span lang="en" dir="ltr" class="mw-content-ltr">gives the wikitext <code><nowiki>start-{{{1|q}}}-endstart-{{{1|q}}}-endstart-{{{1|q}}}-end</nowiki></code></span> <div lang="en" dir="ltr" class="mw-content-ltr"> ==Multilevel substitution== </div> <span lang="en" dir="ltr" class="mw-content-ltr">When substituting a template it may be desirable to carry out a substitution inside the template too.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This can be done with <code>safesubst:</code> in the template.</span> <span lang="en" dir="ltr" class="mw-content-ltr">To prevent premature substitution (i.e., when the template is saved), it is possible to append the {{tag|noinclude|single}} directive after the substitution directive – i.e. <code>safesubst:<noinclude /></code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Another solution is to use the default value of an unused parameter.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Often the empty string works, i.e. <code><nowiki>{{{|safesubst:}}}</nowiki></code>, however it is sometimes used by modules that call templates or for inserting comments.</span> <span lang="en" dir="ltr" class="mw-content-ltr">The difference with <code>subst:<noinclude /></code> is that <code>safesubst:<noinclude /></code>, evaluating to <code>safesubst:</code> not only allows multilevel substitution but also multilevel transclusion, because on transclusion it is ignored.</span> <span lang="en" dir="ltr" class="mw-content-ltr">To make the template such that it allows the choice between these two options as well as one-level substitution (and more choices if more templates, variables, and/or parser functions are involved) one or more parameters are needed, see below.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> == Multilevel substitution with independent control of each substitution separately == </div> <span lang="en" dir="ltr" class="mw-content-ltr">A parameter subst (or more, each with its own name) can be used with <code>safesubst:</code> and the empty string as possible values.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Thus we can for example control whether an inner template is substituted too when the outer template is substituted.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Either possibility can be made the default.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> Inner templates with parameters may control further inner substitutions in the same way; these parameters may depend on the substitution parameter controlling the substitution of the inner template, since if that is not substituted, inner substitutions within that template are not possible. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Similarly, if there are multiple templates, variables, and/or parser functions in the inner template(s) we can control substitution of all, either independently by using different parameters, or with some or all using the same parameter. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> For example, if template <code>T</code> uses parameter <code>subst1</code>: </div> * <span lang="en" dir="ltr" class="mw-content-ltr">with the empty string as default, <code>T</code> calls inner templates and parser functions prefixing their names with <nowiki>{{{subst1|}}}</nowiki>; for calling <code>T</code> we can use:</span> **<nowiki>{{t|..}}</nowiki> – <span lang="en" dir="ltr" class="mw-content-ltr">no substitution</span> **<nowiki>{{subst:t|..}}</nowiki> – <span lang="en" dir="ltr" class="mw-content-ltr">one-level substitution</span> **<nowiki>{{subst:t|subst1=subst:|..}}</nowiki> – <span lang="en" dir="ltr" class="mw-content-ltr">two-level substitution</span> **<nowiki>{{subst:t|subst1=safesubst:|..}}</nowiki> – <span lang="en" dir="ltr" class="mw-content-ltr">ditto</span> * <span lang="en" dir="ltr" class="mw-content-ltr">with default "safesubst:", <code>T</code> calls inner templates and parser functions prefixing their names with <nowiki>{{{subst1|safesubst:}}}</nowiki>; for calling <code>T</code> we can use:</span> **<nowiki>{{t|..}}</nowiki> – <span lang="en" dir="ltr" class="mw-content-ltr">no substitution</span> **<nowiki>{{subst:t|subst1=|..}}</nowiki> – <span lang="en" dir="ltr" class="mw-content-ltr">one-level substitution</span> **<nowiki>{{subst:t|..}}</nowiki> – <span lang="en" dir="ltr" class="mw-content-ltr">two-level substitution</span> <span lang="en" dir="ltr" class="mw-content-ltr">To transfer the choice of substituting or not to templates and parser functions called inside the inner templates of <code>T</code>, we can add to the call of these inner templates something of the form subst2=<nowiki>{{{subst1|}}}</nowiki> or subst2=<nowiki>{{{subst1|safesubst:}}}</nowiki>, respectively ({{ll|Help:variable|variables}} and {{ll|Help:parser function|parser functions}} don't get the additional parameter).</span> <div lang="en" dir="ltr" class="mw-content-ltr"> ===Partial substitution=== </div> <span lang="en" dir="ltr" class="mw-content-ltr">Using a template prepared for optional <code>subst=subst:</code> only with ordinary substitution, without specifying parameter values, allows to insert its code into another template, like copy and paste, but all {{tag|noinclude|open}} parts and {{tag|includeonly|open}} keywords automatically stripped.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Executing inserted code instead of calling it may be more efficient for the server.</span> <span lang="en" dir="ltr" class="mw-content-ltr">A typical example for this technique is expanding, within another template, a template used as test expression in a [[m:ParserFunctions#.23switch:|#switch:]] like {{tim|len}}:</span> # <span lang="en" dir="ltr" class="mw-content-ltr">Development code:</span><br />{{tmpl|0=<code><nowiki>{{#switch: {{len|</nowiki>$1<nowiki>}}|0=</nowiki>$2<nowiki>}}</nowiki></code>|''parameter tag''|''case 0 etc.''}} # <span lang="en" dir="ltr" class="mw-content-ltr">Standard solution:</span><br />{{tmpl|0=<code><nowiki>{{{{{subst|}}}#switch: {{{{{subst|}}}len|</nowiki>$1<nowiki>|subst={{{subst|}}}}}|0=</nowiki>$2<nowiki>}}</nowiki></code>|''parameter tag''|''case 0 etc.''}} # <span lang="en" dir="ltr" class="mw-content-ltr">Better solution: create template code by applying substitution using this wikitext:</span><br />{{tmpl|0=<code><nowiki>{{{{{subst|}}}#switch: {{subst:len|</nowiki>$1<nowiki>}}|0=</nowiki>$2<nowiki>}}</nowiki></code>|''parameter tag''|''case 0 etc.''}} <div lang="en" dir="ltr" class="mw-content-ltr"> {{tim|len}} is prepared for optional substitution, therefore both solutions work, but the latter solution substituting its code is simpler and more efficient. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> See {{tim|csn}} and {{tim|lz12}} for cases where <code>Template:len</code> was substituted in this way. </div> <span lang="en" dir="ltr" class="mw-content-ltr">If a template uses a parameter whose name is an expression containing a template or parser function, and the template is called with a corresponding parameter definition (in terms of the final name of the parameter) it expands properly only if at the time of expansion of the template the expression for the name of the parameter is or has been evaluated.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Thus if the template is substituted without substituting the expression for the parameter name, the parameter definition is "lost", so the parameter becomes undefined.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Therefore in such a case no substitution can give the same rendered result as full substitution, while partial substitution gives a different result.</span> <span lang="en" dir="ltr" class="mw-content-ltr">See e.g. {{tim|ts1}}.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> ===Composite operations=== </div> <span lang="en" dir="ltr" class="mw-content-ltr">By <code><nowiki>{{A|{{B|p}}}}</nowiki></code> a template <code>A</code> is called with, as parameter, a call of template <code>B</code> with a parameter <code>p</code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">We could integrate such template calls to a single call <code><nowiki>{{C|p}}</nowiki></code> of a "[[w:Function composition|composite template]]" <code>C</code> with parameter <code>p</code>.</span> ; <span lang="en" dir="ltr" class="mw-content-ltr">The wikitext for template <code>C</code> would be <code><nowiki>{{A|{{B|{{{1}}}}}}}</nowiki></code>, or with optional substitution the following construct</span> : ;<big>{{((}}<b><small> {{(((}}subst</small><code>|</code><small>{{)))}} </small></b>A|{{((}}<b><small> {{(((}}subst</small><code>|</code><small>{{)))}} </small></b>B|{{{1}}}<b><small> |subst={{(((}}subst</small><code>|</code><small>{{)))}} </small></b>{{))}}<b><small> |subst={{(((}}subst</small><code>|</code><small>{{)))}} </small></b>{{))}}</big> ; <span lang="en" dir="ltr" class="mw-content-ltr">The <code>subst=<nowiki>{{{subst|}}}</nowiki></code> is only necessary for recursive substitution as explained above.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Note that it is not useful to specify <code><nowiki>{{subst|subst:}}</nowiki></code> since in the substitution phase this tag does not reduce to the default <code>subst:</code>.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> If <code>A</code> and/or <code>B</code> is predefined the construct is similar, but without <code>subst=<nowiki>{{{subst|}}}</nowiki></code> for that template. </div> == includeonly == <span lang="en" dir="ltr" class="mw-content-ltr">An alternative method to prevent premature substitution, known as ''"includeonly subst magic"'', is with a pair of includeonly tags.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Substitution is prevented by having the template call inside these tags.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Substitution is also prevented by having one or both tags anywhere in the template call except inside a parameter definition.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Thus the tag(s) can be before, inside, or after <code>safesubst:</code> or <code>subst:</code>, or inside or after the template name.</span> <span lang="en" dir="ltr" class="mw-content-ltr">The positions of the two tags only influence the rendering of the template page itself.</span> <span lang="en" dir="ltr" class="mw-content-ltr">The form {{tmpl|0=<code>{{((}}{{^(}}includeonly{{)^}}safesubst:{{^(}}/includeonly{{)^}}$1{{))}}</code>|something}} suggests that substitution is prevented by discarding <code>safesubst:</code> on the page itself, but actually substitution is prevented because the safesubst-syntax is disturbed by the tags.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> It doesn't substitute ''"something"'' at the time of the creation of the relevant template, but has the desired effect when the template is substituted. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> For examples see "preload" in {{ll|Extension:InputBox#Parameters|Extension:InputBox}} and "substitution" in {{ll|Help:Magic_words#Transclusion modifiers|Help:Magic words}}. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> == Creating a page which applies substitution on the next save == </div> <div lang="en" dir="ltr" class="mw-content-ltr"> See {{ll|Manual:Recursive conversion of wikitext}}. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> ==Forced substitution== </div> <span lang="en" dir="ltr" class="mw-content-ltr">Some templates deliberately refuse invocation without substitution.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This technique is essential for templates producing timestamps of the time they were (first) called, e.g. to display them (like the English Wikipedia’s [[w:Template:Proposed deletion|Proposed deletion]] and [[w:Template:CURRENTISOTIME|CURRENTISOTIME]] templates) or add including pages to dated categories.</span> <!-- [[w:Category:Wikipedia substituted templates]] has many more examples, including [[w:Template:Currentuser]]. --> ; <span lang="en" dir="ltr" class="mw-content-ltr">The following code in any template <code>T</code> outputs a warning unless recursive substitution with <code>subst=subst:</code> is in effect</span>: :<code><nowiki>{{{{{subst|}}}ifdef|{{{{{subst|subst:}}}ns:0}}|'''Warning'''}}</nowiki></code>. :# <span lang="en" dir="ltr" class="mw-content-ltr">Output for <code>{{((}}T{{))}}</code> or <code>{{((}}subst:T{{))}}</code></span> – '''Warning''' :# <span lang="en" dir="ltr" class="mw-content-ltr">Output for <code>{{((}}T|subst=subst:{{))}}</code></span> – <code>{{ {{{subst|}}}ifdef|{{ {{{subst|subst:}}}ns:0}}|'''Warning'''}}</code> :# <span lang="en" dir="ltr" class="mw-content-ltr">Output for <code>{{((}}subst:T|subst=subst:{{))}}</code></span> – <span lang="en" dir="ltr" class="mw-content-ltr">nothing (no remaining wikitext)</span> ; <span lang="en" dir="ltr" class="mw-content-ltr">This is a rare case where replacing <code>[[m:Template:ifdef|ifdef]]</code> by <code>#if:</code> doesn't work directly.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> ==Substitution of part of the parameters== </div> <span lang="en" dir="ltr" class="mw-content-ltr">Let template <code>Feelings</code> use parameters 1 and 2.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Consider creating a template Emotions with one parameter 1, corresponding to <code>Feelings</code>, with a given value <code>love</code> of parameter 2.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Compare <code><nowiki>{{Feelings|2=love}}</nowiki></code> and <code><nowiki>{{Feelings|1={{{1}}}|2=love}}</nowiki></code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">They look the same on the template page, see e.g. <code>{{tim|t ps}}</code>, but the first does not work because <code>{{{1}}}</code> is treated as text, not as parameter.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> However, with substitution (using <code>subst:</code> or [[Special:Expandtemplates]]) the resulting wikitext is the same, without distinction between a text {{{1}}} and a parameter, it is a parameter anyway, so <code>1={{{1}}}</code> is not needed. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> If <code>Feelings</code> contains e.g. <code>#expr</code> with an expression containing both parameters the same applies, except that we can only substitute the highest level (<code>Feelings</code>), not the parser function, so we cannot use <code>Special:Expandtemplates</code>. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> In general, substituting a parameter and applying a template or parser function sometimes gives the same result as substituting the template or parser function with the triple-braced parameter code and then substituting the parameter. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Without defaults (all rendered the same in one-step substitution as without substitution): </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Examples with equality: </div> * <span lang="en" dir="ltr" class="mw-content-ltr"><code>Feelings</code> template containing <code>With {{{1}}} one can {{{2}}}</code></span> ** <span lang="en" dir="ltr" class="mw-content-ltr">When substituted with <code>1=love</code>, <code>2=help</code>, it gives <code>With love one can help</code>.</span> ** <span lang="en" dir="ltr" class="mw-content-ltr">When substituted with <code>2=help</code>, it gives <code>With {{{1}}} one can help</code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This itself, when substituted with <code>1=compassion</code>, it gives <code>With compassion one can help</code>.</span> * <span lang="en" dir="ltr" class="mw-content-ltr">Two-level substitution of a template containing <code><nowiki>{{#if:{{{4}}}|{{{3}}}p}}</nowiki></code>.</span> ** <span lang="en" dir="ltr" class="mw-content-ltr">When substituted with <code>3=u</code>, <code>4=v</code>, it gives <code>up</code>.</span> ** <span lang="en" dir="ltr" class="mw-content-ltr">When substituted with <code>4=v</code>, it gives <code>{{{3}}}p</code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This itself, when substituted with <code>3=u</code>, it gives <code>up</code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Examples without equality:</span> * <span lang="en" dir="ltr" class="mw-content-ltr">Two-level substitution of a template containing <code><nowiki>{{#if:{{{3}}}|{{{4}}}p}}</nowiki></code></span> ** <span lang="en" dir="ltr" class="mw-content-ltr">When substituted with <code>3=</code>, <code>4=v</code>, it gives the empty string.</span> ** <span lang="en" dir="ltr" class="mw-content-ltr">When substituted with <code>4=v</code>, it gives <code>vp</code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This itself, when substituted with <code>3=u</code>, it remains <code>vp</code>.</span> * <span lang="en" dir="ltr" class="mw-content-ltr">Two-level substitution of a template containing <code><nowiki>{{#if:{{{2}}}|{{{1}}}p}}</nowiki></code></span> ** <span lang="en" dir="ltr" class="mw-content-ltr">When substituted with <code>1=u</code>, <code>2=v</code>, it gives <code>up</code>.</span> ** <span lang="en" dir="ltr" class="mw-content-ltr">When substituted with <code>2=v</code>, it gives <code>{{{1}}}pp</code> (the bug).</span> <span lang="en" dir="ltr" class="mw-content-ltr">This itself, when substituted with <code>3=u</code>, it gives <code>upp</code>.</span> * <span lang="en" dir="ltr" class="mw-content-ltr">Two-level substitution of a template containing <code><nowiki>{{#expr:{{{1}}}*{{{2}}}}}</nowiki></code></span> ** <span lang="en" dir="ltr" class="mw-content-ltr">When substituted with <code>1=7</code>, <code>2=8</code>, it gives <code>56</code>.</span> ** <span lang="en" dir="ltr" class="mw-content-ltr">When substituted with <code>2=8</code>, it gives <strong class="error">Expression error: Unrecognised punctuation character "{"</strong>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This itself, when substituted with <code>1=7</code>, it remains the same.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Thus without equality we may or may not get an error message.</span> <span lang="en" dir="ltr" class="mw-content-ltr">One example shows that substitution of one parameter can be affected by the bug mentioned above.</span> <span lang="en" dir="ltr" class="mw-content-ltr">However, we can then replace e.g. <code>{{{1}}}</code> by <code><nowiki>{{{1{{{{{substvoid|}}}void}}}}}</nowiki></code> and do full substitution, except that <code>substvoid</code> is undefined, preventing the bug.</span> <span lang="en" dir="ltr" class="mw-content-ltr">The result works already correctly with transclusion.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Subsequently it can be substituted with <code>substvoid=subst:</code> so that we get the plain <code>{{{1}}}</code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">With defaults:</span> <span lang="en" dir="ltr" class="mw-content-ltr">Rendered the same as without substitution:</span> * <span lang="en" dir="ltr" class="mw-content-ltr">Two-level substitution of a template containing <code><nowiki>With {{{1|love}}} one can {{{2}}}</nowiki></code> with <code>2=help</code> gives <code><nowiki>With {{{1|love}}} one can help</nowiki></code>.</span> * <span lang="en" dir="ltr" class="mw-content-ltr">Two-level substitution of a template containing <code><nowiki>{{#if:{{{4}}}|{{{3|d}}}p}}</nowiki></code> with <code>4=v</code> gives <code>{{{3|d}}}p</code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Not rendered the same as without substitution:</span> * <span lang="en" dir="ltr" class="mw-content-ltr">Two-level substitution of a template containing <code><nowiki>{{#if:{{{3|}}}|{{{4}}}p}}</nowiki></code> with <code>4=v</code> gives <code>vp</code>.</span> * <span lang="en" dir="ltr" class="mw-content-ltr">Two-level substitution of a template containing <code><nowiki>{{#if:{{{2}}}|{{{1|d}}}p}}</nowiki></code> with <code>2=v</code> gives <code>{{{1|d}}}pp</code> (the bug).</span> * <span lang="en" dir="ltr" class="mw-content-ltr">Two-level substitution of a template containing <code><nowiki>{{#expr:{{{1|6}}}*{{{2}}}}}</nowiki></code> with <code>2=8</code> gives:</span> <strong class="error">Expression error: Unrecognised punctuation character "{"</strong> <span lang="en" dir="ltr" class="mw-content-ltr">After substitution with the parameter definition:</span> * <code><nowiki>{{subst:#if:{{{3|}}}|vp}}</nowiki></code> → <code>vp</code> * <code><nowiki>{{subst:#if:v|{{{1|d}}}p}}</nowiki></code> → <code>{{{1|d}}}pp</code> <span lang="en" dir="ltr" class="mw-content-ltr">(the bug)</span> * <code><nowiki>{{subst:#expr:{{{1|6}}}*8}}</nowiki></code> → <strong class="error">Expression error: Unrecognised punctuation character "{"</strong> <span lang="en" dir="ltr" class="mw-content-ltr">Rewritten:</span> * <code><nowiki>{{subst:#if:{{subst:#ifeq:{{{3|+}}}|{{{3|-}}}|vp}}}}</nowiki></code> → <span lang="en" dir="ltr" class="mw-content-ltr">the empty string</span> * <code><nowiki>{{subst:#if:v|{{subst:#ifeq:{{{1|+}}}|{{{1|-}}}|{{{1}}}|d}}p}}</nowiki></code> → <code>dp</code> * <code><nowiki>{{subst:#expr:{{subst:#ifeq:{{{1|+}}}|{{{1|-}}}|{{{1}}}|6}}*8}}</nowiki></code> → <code>48</code> <div lang="en" dir="ltr" class="mw-content-ltr"> ==Limitations== </div> <span lang="en" dir="ltr" class="mw-content-ltr">Substitution is not available inside parser tags like {{tag|ref}} and {{tag|gallery}}.</span> <span lang="en" dir="ltr" class="mw-content-ltr">If you write {{tnull|subst:foo}}, it is not substituted nor transcluded, but remains as-is.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> ==Documenting substitution== </div> <span lang="en" dir="ltr" class="mw-content-ltr">Usage of a template through <code>subst:</code> does not automatically show up in page histories.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Therefore providing the line of wikitext containing "subst:" in the [[Special:MyLanguage/Help:Edit summary|edit summary]] is especially useful.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Also pages with a substituted template do not show up in backlinks, and the template does not appear in the list of transcluded templates on the edit page.</span> <span lang="en" dir="ltr" class="mw-content-ltr">The template could add pages to a category to track substitutions, but listing this category on a page may clutter the list of content-based categories the page is in.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Also, comments outside noinclude tags are included in the wikitext.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Thus a comment can be used to mention the template.</span> <span lang="en" dir="ltr" class="mw-content-ltr">It can even contain the values of the parameters, because substitution of parameters works even in comments.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> ==See also== </div> *{{ll|Help:Substitution/tl;dr}} – [[wikt:tl;dr|tl;dr]] * <span lang="en" dir="ltr" class="mw-content-ltr">[[w:Help:Substitution]]</span> ** <span lang="en" dir="ltr" class="mw-content-ltr">[[w:Help:Substitution#The safesubst: modifier|The safesubst: modifier]]</span> * <span lang="en" dir="ltr" class="mw-content-ltr">[[w:Wikipedia:Template substitution]]</span> – <span lang="en" dir="ltr" class="mw-content-ltr">partly technical, partly policy</span> *[[Phabricator:T4003]] – <span lang="en" dir="ltr" class="mw-content-ltr">feature request to allow marking a template as being substituted without <code>subst:</code></span> * <span lang="en" dir="ltr" class="mw-content-ltr">Templates containing a call to itself with <code>subst:</code> and producing a similar call with updated info, either replacing or adding to the previous info:</span> **{{tim|last edit}} **{{tim|page history}} * <span lang="en" dir="ltr" class="mw-content-ltr">The [[Special:MyLanguage/Help:Links#Pipe_trick|pipe trick]], which behaves similarly to template substitution</span> [[Category:Templates{{#translation:}}]]
Search
Search
Export translations
Add topic