"साँचा:If empty/doc" के अवतरणों में अंतर

मुक्त ज्ञानकोश विकिपीडिया से
नेविगेशन पर जाएँ खोज पर जाएँ
imported>SM7
छो (en:Template:If_empty/doc से 45 अवतरण आयात किये गए)
 
छो (१ अवतरण आयात किया गया)
 
(एक अन्य सदस्य द्वारा किया गया बीच का एक अवतरण नहीं दर्शाया गया)
पंक्ति १: पंक्ति १:
{{Documentation subpage}}
{{Documentation subpage}}
{{Template redirects |Ifempty |First nonempty |Use first nonempty}}
<!----PLEASE ADD TEMPLATE CATEGORIES WHERE INDICATED AT THE BOTTOM OF THIS PAGE---->
{{Template redirect |Ifempty |First nonempty |Use first nonempty |Firstdata}}
{{Lua|Module:If empty}}
{{Lua|Module:If empty}}
{{High-risk | 640000+ }}
{{High-use|all-pages = yes}}
<!----PLEASE ADD CATEGORIES WHERE INDICATED AT THE BOTTOM OF THIS PAGE---->


This is the {{tl|If empty}} template.
This template is used inside other templates. It takes any number of parameters and returns the first found to be defined and non-empty. If none is found, nothing is returned. By writing <code><nowiki>{{if empty|a|b|c}}</nowiki></code> instead of <code><nowiki>{{#if:a|a|{{#if:b|b|c}}}}</nowiki></code>, expression a and b do not have to be [[Don't repeat yourself|repeated]] and evaluated twice. The template provides a [[fallback]] order, similar to a [[try catch]] based "It is easier to ask for forgiveness than permission" ([[EAFP]]) programming style.


*''Update 2014 Dec 23'': This template used to only take 9 parameters. This limit has been removed.
Typical usage is like this:
 
This template is used inside other templates. It takes any number of parameters and returns the first found to be defined and non-empty. If none is found, nothing is returned. Typical usage is like this:
: <code><nowiki>{{If empty |{{{logo|}}} |{{{image|}}} |{{{picture|}}} |default.svg}}</nowiki></code>
: <code><nowiki>{{If empty |{{{logo|}}} |{{{image|}}} |{{{picture|}}} |default.svg}}</nowiki></code>
: <nowiki>("Return {{{logo|}}} if it has a value; else {{{image|}}} if it has a value; else {{{picture|}}} if it has a value; else return "default.svg".")</nowiki>
: <nowiki>("Return {{{logo|}}} if it has a value; else {{{image|}}} if it has a value; else {{{picture|}}} if it has a value; else return "default.svg".")</nowiki>
This returns the first of the parameters ''logo'' and ''image'' that is defined and non-empty, otherwise "default.svg".
This returns the first of the parameters ''logo'', ''image'' and ''picture'' that is defined and non-empty, otherwise "default.svg".


===Rationale===
===Rationale===
पंक्ति २३: पंक्ति २१:
But this becomes complex when several parameters are to be checked:
But this becomes complex when several parameters are to be checked:
: <code><nowiki>{{#if:{{{logo|}}} |{{{logo}}} |{{#if:{{{image|}}} |{{{image}}} |{{#if:{{{picture|}}} |{{{picture}}} |default.svg}} }} }}</nowiki></code>
: <code><nowiki>{{#if:{{{logo|}}} |{{{logo}}} |{{#if:{{{image|}}} |{{{image}}} |{{#if:{{{picture|}}} |{{{picture}}} |default.svg}} }} }}</nowiki></code>
: <nowiki>("If {{{logo}}} has a value, return it; else if {{{image}}} has a value, return that; else if {{{picture}}} has a value, return that; else return "default.svg".)</nowiki>
: <nowiki>("If {{{logo}}} has a value, return it; else if {{{image}}} has a value, return that; else if {{{picture}}} has a value, return that; else return "default.svg".")</nowiki>
In these cases, {{tlf|if empty}} produces the simpler syntax (as above):
In these cases, {{tlf|if empty}} produces the simpler syntax (as above):
: <code><nowiki>{{if empty |{{{logo|}}} |{{{image|}}} |{{{picture|}}} |default.svg}}</nowiki></code>
: <code><nowiki>{{if empty |{{{logo|}}} |{{{image|}}} |{{{picture|}}} |default.svg}}</nowiki></code>


====Piping====
====Piping====
Parameters used with {{tlf|if empty}} must be [[Pipe (Unix)|pipe]]d&nbsp;– i.e. include the [[Vertical bar|vertical bar (pipe) symbol]] {{nowrap|("{{thinsp}}&#124;{{thinsp}}")}} as a trailing character&nbsp;– so that empty or undefined parameters aren't treated as text and returned incorrectly. Hence, for example, <code><nowiki>{{{logo|}}}</nowiki></code>, <code><nowiki>{{{image|}}}</nowiki></code> and <code><nowiki>{{{picture|}}}</nowiki></code> rather than {{param|logo}}, {{param|image}} and {{param|picture}} in the above examples.
Parameters used with {{tlf|if empty}} must be [[Pipe (Unix)|pipe]]d&nbsp;– i.e. include the [[Vertical bar|vertical bar (pipe) symbol]] {{nowrap|("{{thin space}}&#124;{{thin space}}")}} as a trailing character&nbsp;– so that empty or undefined parameters aren't treated as text and returned incorrectly. Hence, for example, <code><nowiki>{{{logo|}}}</nowiki></code>, <code><nowiki>{{{image|}}}</nowiki></code> and <code><nowiki>{{{picture|}}}</nowiki></code> rather than {{param|logo}}, {{param|image}} and {{param|picture}} in the above examples.


===Examples===
===Examples===
पंक्ति ३५: पंक्ति ३३:
  |row1style=background:whitesmoke;padding-left:0.5em;font-weight:bold;
  |row1style=background:whitesmoke;padding-left:0.5em;font-weight:bold;
  | Code | Result |
  | Code | Result |
  | {{tc|if empty}} | {{if empty}} | Returns an empty string.
  | {{tlc|if empty}} | {{if empty}} | Returns an empty string.
  | {{tc|if empty|one}} | {{if empty|one}} | Returns the first parameter that is defined and not empty.
  | {{tlc|if empty|one}} | {{if empty|one}} | Returns the first parameter that is defined and not empty.
  | {{tc|if empty|one|two}} | {{if empty|one|two}} |  
  | {{tlc|if empty|one|two}} | {{if empty|one|two}} |  
  | {{tc|if empty|one|two|three|four}} | {{if empty|one|two|three|four}} |  
  | {{tlc|if empty|one|two|three|four}} | {{if empty|one|two|three|four}} |  
  | {{tc|if empty||two}} | {{if empty||two}} | The first parameter is empty/undefined, so is passed over.
  | {{tlc|if empty||two}} | {{if empty||two}} | The first parameter is empty/undefined, so is passed over.
  | {{tc|if empty||two|three|four}} | {{if empty||two|three|four}} |  
  | {{tlc|if empty||two|three|four}} | {{if empty||two|three|four}} |  
  | {{tc|if empty||two||four}} | {{if empty||two||four}} |  
  | {{tlc|if empty||two||four}} | {{if empty||two||four}} |  
  | <code><nowiki>{{if empty||||||||||ten}}</nowiki></code> | {{if empty||||||||||ten}} | Unlike in previous versions, this template is no longer limited by 9 parameters.
  | <code><nowiki>{{if empty||||||||||ten}}</nowiki></code> | {{if empty||||||||||ten}} | Unlike in previous versions, this template is no longer limited by 9 parameters.
  | {{tc|if empty|}} | {{if empty|}} | The only parameter is empty or undefined, so returns an empty string.
  | {{tlc|if empty{{!}}}} | {{if empty|}} | The only parameter is empty or undefined, so returns an empty string.
  | {{tc|if empty||||}} | {{if empty||||}} | Returns an empty string.
  | {{tlc|if empty|{{!}}{{!}}{{!}}{{!}}}} | {{if empty||||}} | Returns an empty string.
  | <code><nowiki>{{if empty|{{{1|}}}|{{{2|}}}|three}}</nowiki></code> | {{if empty|{{{1|}}}|{{{2|}}}|three}} |  
  | <code><nowiki>{{if empty|{{{1|}}}|{{{2|}}}|three}}</nowiki></code> | {{if empty|{{{1|}}}|{{{2|}}}|three}} |  
  | <code><nowiki>{{if empty|{{{1}}}|{{{2}}}|three}}</nowiki></code> | {{if empty|{{{1}}}|{{{2}}}|three}} | No pipe-characters following the names of the first two parameters ("1" and "2"), so the first of these returned as text ("{{{1}}}").
  | <code><nowiki>{{if empty|{{{1}}}|{{{2}}}|three}}</nowiki></code> | {{if empty|{{{1}}}|{{{2}}}|three}} | No pipe-characters following the names of the first two parameters ("1" and "2"), so the first of these returned as text ("{{{1}}}").
पंक्ति ५६: पंक्ति ५४:
* {{tl|if either}}
* {{tl|if either}}
* [[Help:Parameter default]]
* [[Help:Parameter default]]
* {{tl|Category see also if exists 2}}


<includeonly>{{#ifeq:{{SUBPAGENAME}}|sandbox |
<includeonly>{{Sandbox other|
| <!-- ADD CATEGORIES BELOW THIS LINE, PLEASE: -->
| <!-- ADD TEMPLATE CATEGORIES BELOW THIS LINE, PLEASE: -->
[[Category:If-then-else templates]]
[[Category:If-then-else templates]]
[[Category:Parameter selection templates]]
}}</includeonly>
}}</includeonly>

१३:२९, १ जुलाई २०२२ के समय का अवतरण

{{

 साँचा:namespace detect

| type = notice | image = | imageright = | class = | style = margin-bottom:1.0em; | textstyle = | text = साँचा:strong for साँचा:terminate sentence
It contains usage information, categories and other content that is not part of the original साँचा page. | small = | smallimage = | smallimageright = | smalltext = | subst = | date = | name = }}

This template is used inside other templates. It takes any number of parameters and returns the first found to be defined and non-empty. If none is found, nothing is returned. By writing {{if empty|a|b|c}} instead of {{#if:a|a|{{#if:b|b|c}}}}, expression a and b do not have to be repeated and evaluated twice. The template provides a fallback order, similar to a try catch based "It is easier to ask for forgiveness than permission" (EAFP) programming style.

Typical usage is like this:

{{If empty |{{{logo|}}} |{{{image|}}} |{{{picture|}}} |default.svg}}
("Return {{{logo|}}} if it has a value; else {{{image|}}} if it has a value; else {{{picture|}}} if it has a value; else return "default.svg".")

This returns the first of the parameters logo, image and picture that is defined and non-empty, otherwise "default.svg".

Rationale

The MediaWiki parameter default function doesn't return the default value for empty parameters. That is, {{{logo|default.svg}}} does not return "default.svg" if the template was called like this: {{template|logo=}}.

The usual workaround for a single parameter is:

{{#if:{{{logo|}}} |{{{logo}}} |default.svg}}
("If {{{logo}}} has a value, return it, else return "default.svg".")

But this becomes complex when several parameters are to be checked:

{{#if:{{{logo|}}} |{{{logo}}} |{{#if:{{{image|}}} |{{{image}}} |{{#if:{{{picture|}}} |{{{picture}}} |default.svg}} }} }}
("If {{{logo}}} has a value, return it; else if {{{image}}} has a value, return that; else if {{{picture}}} has a value, return that; else return "default.svg".")

In these cases, साँचा:tlf produces the simpler syntax (as above):

{{if empty |{{{logo|}}} |{{{image|}}} |{{{picture|}}} |default.svg}}

Piping

Parameters used with साँचा:tlf must be piped – i.e. include the vertical bar (pipe) symbol साँचा:nowrap as a trailing character – so that empty or undefined parameters aren't treated as text and returned incorrectly. Hence, for example, {{{logo|}}}, {{{image|}}} and {{{picture|}}} rather than साँचा:param, साँचा:param and साँचा:param in the above examples.

Examples

स्क्रिप्ट त्रुटि: "aligned table" ऐसा कोई मॉड्यूल नहीं है।

See also