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  Indentation  







2 Performance considerations  





3 See also  














Template:If







Anarâškielâ
Аԥсшәа
العربية
ܐܪܡܝܐ
Arpetan

Авар
Azərbaycanca
تۆرکجه
Basa Bali

Беларуская

Bikol Central
Bosanski
Буряад
Català
Чӑвашла
Dansk
ދިވެހިބަސް
Dolnoserbski
Español
Euskara
فارسی
Føroyskt
Français
Gaeilge
ГӀалгӀай

Hausa
Հայերեն
Ilokano
Bahasa Indonesia
IsiZulu
Íslenska
Italiano
עברית

Қазақша
Kurdî
Кыргызча
Ladino
Latina
Latviešu
ि
Македонски

مصرى

Bahasa Melayu
Мокшень


Нохчийн
Norsk bokmål
Norsk nynorsk
Occitan
ି
Oʻzbekcha / ўзбекча

پښتو

Reo tahiti
Română
Русский

Scots
Sesotho sa Leboa
Shqip

Simple English
سنڌي
Slovenčina
Slovenščina
کوردی
Српски / srpski
Suomi
Svenska
ி
Татарча / tatarça
 


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




Betawi
 

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
MediaWiki
Meta-Wiki
Wikispecies
Wikibooks
Wikidata
Wikifunctions
Wikinews
Wikiversity
 
















Appearance
   

 





Permanently protected template

From Wikipedia, the free encyclopedia
 


{{ safesubst:p{{ safesubst:#if{{{1}}}:{{{2}}}|1|2}}|{{{3}}}|}}

With this template the parser functions of the ParserFunctions collection with names starting with "#if" can be used in a way such that they do not strip spaces and newlines from the "then" and "else" part. Spaces still do not affect the outcome of the condition. Parameter 1 selects the if-type as "eq", "expr", "exist" or "error" (for #iferror), or empty "||" for a simple if-there (for #if). The template can be repeatedly nested 6 or 7 levels, one inside the other, because the outer-most is completed before running either the then/else inner levels.

This template can be substituted, when the expression or comparison will not change. Functionally, a lead-space character is stored, internally, as a simple blank character. Note, in many cases a null nowiki tag ("<nowiki/>") could be used, without Template:If, to allow a lead-space (such as "<nowiki/> xx") anywhere, but the internal storage puts a 43-character marker for "<nowiki/>" in string length. However, there is no extra expansion depth for a nowiki-tag, such as in trailing space, "zz <nowiki/>".

Usage

Where p is output for a true statement and q is output for a false statement.

Compare:

For example, with a template named Template:Template name containing the code {{#ifeq: {{{parameter}}} |u| p | q }}, entering the code {{Template name|parameter=u}} into an article gives "p", but {{Template name|parameter=v}} gives "q".

The text of either the then-clause or else-clause is only processed and expanded when triggered. Hence, any templates in use are only expanded once the then-clause or else-clause is matched, otherwise they are skipped as merely paired braces, "{{" with "}}".

Indentation

If indenting the markup, care must be taken to avoid extra newlines when indenting "}}" on the next line. An extra bar pipe "|" can be added after the else-clause to complete that text, and allow "}}" to then be placed anywhere without adding a newline into the else-clause. For example:

  • {{if|{{{1|}}}
        |then found parameter 1|else no parameter 1| ← extra "|" ends else-clause
    }}
    

When the else-clause is indented to the next line, a newline (CR/LF) is added:

  • {{if|{{{1|}}}
        |then found parameter 1 ← extra newline here
        |else noparameter 1|
    }}
    

To indent the else-clause, split an HTML-form comment, as "<!--" with next line as "-->|else...". Unless each then-clause and else-clause is carefully tested, to watch for extra newlines, then the results are likely to cause broken lines, with extra line breaks for each newline. For that reason, a global edit with simple search-and-replace of "{#if:" to "{if||" is likely to leave newline problems, wherever the original markup was wrapped to indent either the else-clause or "}}" of each if-structure. Indenting the then-clause is not a problem.

Performance considerations

Because Template:If must prepare the parameters for #if, #ifeq, #ifexpr, (etc.) there is a slight overhead when using it. Each nested use adds 5 levels to the template expansion depth, so 7 nested if-templates would use 35 levels (5*7) of the 41-level limit.

Using P-if syntax: A similar if-structure can be coded without Template:If, by using the {{P1}} and {{P2}} templates in a "P-if" structure. Template:P1 always returns parameter 1, and P2 returns the 2nd. So, a comparison of 4 versus 3 can be coded using P-if syntax form:

Example of #ifexpr: "{{P{{#ifexpr: 4 > 3|1|2}}| then 4 greater| else 4 lower}}"
Result for #ifexpr:   " then 4 greater" ← the comparison invoked {{P1}}.

The expansion depth of a P-if is only 4 levels deep, but nesting of the then-clause or else-clause might be more confusing than using nested levels of Template:if.

Example of #if: "{{P{{#if:{{{2|x}}}|1|2}}| then {2} set| else {2} empty}}"
Result for #if: " else {2} empty" ← the comparison invoked {{P2}}.

See also


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

Category: 
If-then-else templates
Hidden category: 
Wikipedia fully protected templates
 



This page was last edited on 17 February 2024, at 05:14 (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