මෙම පිටුව ඉංග්රීසි ව්යාපෘතියෙන් මෙහි ගෙනවිත් ඇත. අන්තර්ගතය අපගේ ව්යාපෘතිය සමඟ 100% නොගැලපෙනමුත් අපගේ අවශ්යතාවයන් වලට අනුකූලව ගොඩනගා ගැනීම සඳහා මූලික පියවර ලෙස භාවිතා කළ හැකිය. |
මෙම උදවු පිටුව කෙලෙසද-මාර්ගෝපදේශනයකි. මෙය විකිපීඩියාවේ ප්රතිමාන සහ ක්රියාමාර්ගවල සමහර ලක්ෂණ විස්තර කරයි. එය විකිපීඩියාවේ ප්රතිපත්ති සහ මාර්ගෝපදේශන අතුරින් එකක් නොවේ, and may reflect varying levels of consensus and vetting. මෙම පිටුව තුල කිසිවක් අසංගත නම් එම ප්රතිපත්ති සහ මාර්ගෝපදේශන වෙත යොමු වන්න |
There are some advanced template coding techniques to improve the display or editing of templates in Wikipedia. There are also some tactics for debugging template parameters in the MediaWiki markup language. If there is a possibility, it is better to use lua modules.
Many bugs are associated with the difficulty in dealing with some awkward features in the markup language which lead to coding errors. Unbalanced metacharacters are a major source of errors. For example, coding {{1}}}
instead of {{{1}}}
causes it to act as if it were {{1}} }
, thereby invoking Template:1 + "}".
There are some differences of wiki-formatting of contents of parameters when inside #if-expressions, but not when outside. Templates which need to be substituted need special handling. Providing parameter defaults or parameter aliases is covered as well.
Inside a single template, the nesting limit is 40 nested expressions, such as 40 multiple "if-then-else-if...". At the 41st nested "if" keyword an error message might appear, as: "Exceeded nesting limit". However, when not nested beyond 40 levels, a template can contain hundreds of if-expressions and switch-branches, but not all nested inside the others.
Some templates have contained complex conditional calculations nested over 23 levels deep, for years. Also, some templates have contained hundreds of if-expressions, for years, just NOT all nested as one, giant: if-then-else-else-else-else-else....
An issue which complicates template processing, for parameters, is the wiki-formatting of parameter contents when inside of if-logic (such as #if or #ifeq) or #switch (or lc:, lcfirst:, uc:, ucfirst:). මැයි 2012 වන විට[update], the MediaWiki markup parser is still wiki-formatting the contents of parameters when inside #if-expressions (but not outside). This means that parameters containing spaces, lead semicolon, colon (":") or pound sign ("#") can change their values while inside the if-clauses (surprise!). So, for example, a parameter {{{4}}}
when outside an #if can display differently than inside {{#ifeq:{{{1}}}=0|{{{4}}} ...}}
. The worst shock is when parameter 4 contains a leading semicolon, which triggers formatting to become ye-olde bolded semicolon-header line:
TEST 1: {{#if:{{{4|}}}|{{{4|;}}} <== yes, semicolon|no, 4=empty}}
TEST 2: {{#if:{{{4|;}}}|{{{4|;}}} <== yes, semicolon|no, 4=empty}}
|
The problem occurs whether inside of #if, #ifexpr, #ifeq or #switch markup expressions. If the parameter is preceded by text, in either of the then/else clauses, then the wiki-formatting inside the parameter does not occur.
TEST 3: {{#ifexpr:{{{1|7}}}=7|<b></b>{{{4|;}}} equals 7|not 7}}
TEST 4: {{#ifexpr:{{{1|7}}} < 9|{{{4|#}}} LESS THAN 9|not<9}}
TEST 5: {{#ifexpr:{{{1|7}}} < 9| {{{4|#}}} LESS THAN 9|not<9}}
TEST 6: "{{#ifexpr:{{{1|7}}} < 9| {{{4|#}}} LESS THAN 9|not<9}}"
|
In TEST 4, the leading pound sign "#" triggered auto-numbering of the line (indented with "1."). The situation of having a leading semicolon, colon or "#" might be relatively rare, but this is just a reminder: for showing the true contents of a template parameter, try to display a parameter outside the start of any if-statement clauses, or display other text before the parameter inside the if-logic, or else prepare for some shocking results when a parameter is wiki-formatted for display inside the if-logic.
If the result of #if, etc. is not intended to be formatted, using #
, :
, and ;
instead of #
, :
, and ;
will work fine.
TEST 7: {{#ifexpr:{{{1|7}}} < 9|{{{4|#}}} LESS THAN 9|not<9}}
|
Many coding errors can be debugged, more easily, by attempting to isolate the code section where coding errors most likely occurred. Intense proofreading, of the logic flow, is most often the quickest fix, such as checking for typical syntax errors (see below: "Common coding errors"). Sometimes, a section of troublesome code could be copied into a short test page, then by edit-preview, tested there, separately. However, if editing that extra-page window seems like too much effort, then consider merely copying the code to the top of the current template. Similarly, a template could be developed, in the early stages, as multiple sections of code, each to be debugged separately, then eventually joined together, such as nested sections with if-then-else-if.
As a review of those options, consider:
The basic strategy: isolate the code section to be debugged.
Next, the testing, of each section of code, is crucial. There are some age-old adages to heed:
Perhaps put a variety of examples on each template's doc subpage, to help detect problems early in development. However, for complex templates, then the talk-page, or a special subpage "/testcases", should contain a section of numerous examples (a whole lot of them) to demonstrate the full scope of template features.
When developing sections of markup that use template parameters, try to always set each parameter with a default value, especially inside expressions or if-logic coding:
{{#expr: 109.75 / {{{1|1}}} }}
→ default {1} as 1 not zero.{{#ifeq: {{{answer|y}}}|y|show yes}}
If a particular parameter is given the same default value across the whole page, then that value could be easily changed, in a text editor, by a global search-and-replace string substitution, to change the default value to some other value, for testing each case.
If those parameters are not given default values, then those sections of code cannot be tested, during edit-preview, while editing the template. Any parameter without a default value will become the literal triple-brace text (such as the literal 7 characters: {{{x}}}
), and non-defaulted parameters cannot be evaluated, in expressions or if-logic, during an edit-preview of the template page.
There are several common coding errors which will cause problems when processing templates. The following can be used as a checklist, to help debug problems, when a template seems to be acting bizarre:
{{{1}}
. Having only 2 closing-braces }}
might treat the parameter as a template named "Template:1" (preceded by a lone "{" brace).<!--
at the start of an HTML comment can cause bizarre results, with no error message. Forgetting the exclamation point is very common: <--
should be <!--
.-->
at the end of an HTML comment can cause bizarre results, with no error message.{{#ifexpr {{{1|y}}}=0|then zero|else not}}
Note that those common coding errors could have been easily spotted by a simple syntax checker, such as warning that 3&2 braces could be trouble: {{{size|180px}}
is treated as "{Template:Size" trying to pass 180px as the parameter because of only 2 end-braces.
Again, always checking first for common errors, as the first step, can avoid time hunting for "complex errors" which never really existed. Remember: the MediaWiki markup language is extremely error-prone, so that's why so many coding errors occur, and that's why:
Many hideous problems truly are merely 1-minute syntax fixes.
In rare cases, a template might need to be rewritten to allow text substitution (per WP:SUBST), where the results of running a template will be saved into the page during an edit-SAVE operation. In that case, the safesubst-prefix must be inserted into every markup function used inside that template, at every level of nested logic. Also, every HTML comment must be surrounded by "noinclude" tags: <noinclude><!--HTML comment HERE--></noinclude>
.[a] Otherwise, all of the triggered HTML comments will be stored inside the SAVEd page, in the sequence executed by running the template. NOTE: All the extra "noinclude" and safesubst:<noinclude/>
keywords will likely require the template's markup to be re-indented inside, to fit all that extra inserted text, which will widen and clutter the original markup style.
Specifically, to alter a template to allow text substitution, then the prefix safesubst:<noinclude/>
must be inserted inside the opening double-brace {{
of each markup function inside that template. Some examples of inserting the safesubst-prefix inside a template's markup:
Total articles now: {{NUMBEROFARTICLES}}
→ 21,701Total articles was: {{ subst:NUMBEROFARTICLES}}
→ 3,953,715{{ safesubst:<noinclude/>#ifeq:{{{1|AX}}}|AX|yes}}
{{ safesubst:<noinclude/>lc:THIS LOWERCASE TEXT}}
→ this lowercase textIn general, every markup function which starts with double-brace {{
must be altered to insert the long safesubst-prefix safesubst:<noinclude/>
(with no space afterward).[b] The action of keyword "safesubst" is to allow conditional substitution of markup, when the whole template is invoked as {{subst:MyTemplate|...}}. In essence, the keyword "safesubst" could be called "ifsubst" as meaning, "if 'subst:' was used to invoke this template, then substitute here as well".
Remember: The safesubst-prefix must be inserted into every markup function inside that template, except for test logic never used in an actual page. Any markup which omits "safesubst" will fail if the template is executed by the subst mode, "{{subst:MyTemplate|...}}". Parameters are not changed, so {{{1}}}
would remain unchanged, without a safesubst-prefix.
Exceptions: Only logic which would never be stored in a page could omit "safesubst", such as test-logic which is triggered by special parameter values never used inside a stored page. Any markup which omits "safesubst" will only work during regular transclusion but fail if the template is executed by using the subst-mode prefix "subst:". For more examples, and further technical explanations, see: WP:Substitution.
Indenting the long lines: All of the added safesubst-prefix text will widen lines, so to improve readability, they could be split and indented before any of the safesubst:<noinclude/>
prefixes. For example:
{{ safesubst:<noinclude/>#ifeq:{{
safesubst:<noinclude/>padleft:|1|{{{1}}} }}|A|Begins with "A"}}
In that indentation style, the text "safesubst:<noinclude/>" begins the next line. Avoid wrapping a line after the prefix safesubst:<noinclude/>
because several markup functions might fail to work correctly unless the safesubst prefix is immediately attached before the keyword, such as {{ safesubst:<noinclude/>#ifeq:...}}
When trying to perform more-complex, or intricate, operations, there might be an instinctive fear that templates cannot be as large as needed. However, there are many very large templates which have been running, for years, on Wikipedia, for example:
The source-footnote formatter, Template:Citation/core, displays a standardized citation format, as invoked by several wrapper templates which pass hundreds of parameters, where the core logic checks 621 parameter values, in the conditional markup expressions.
The Special:ExpandTemplates page takes some wikitext and expands everything in double braces recursively: templates, parser functions like {{#if:...}}, and variables like {{CURRENTDAY}}
<noinclude>
tags.
([^{]){{([^{])
and replace it with \1{{ safesubst:<noinclude/>\2
s/([^{]){{([^{])/\1{{ safesubst:<noinclude/>\2/g