Jump to content

Module:Template parameter value/doc: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Move from template doc page
better docs
Line 1: Line 1:
{{high risk}}
{{high risk}}


Implements {{tl|Template parameter value}}
Implements {{tl|Template parameter value}} and {{tl|HasTemplate}}, and can be used from other modules.


== Module usage ==
== Module functions ==
=== getParameter ===
The module can be used directly for other modules through the function <code>getValue(page, templates, parameters, options)</code>, which will return a success boolean and either the resulting parameter value or why it failed.
getParameter takes 4 arguments: The page name (string), the template/s (string or table of strings), the parameter (string), and an optional options table. It will return either <code>true</code> and the contents of the requested parameter or <code>false</code> and a reason for failure.


The following options are available:
<code>page</code>, <code>templates</code>, and <code>parameters</code> are all required arguments. <code>options</code> is an optional table which can have the parameters <code>template_index</code>, <code>parameter_index</code>, <code>ignore_subtemplates</code>, <code>only_subtemplates</code>, and <code>ignore_blank</code>. More info on what these parameters do can be found in the template's [[Template:Template parameter value#Template data|template data]].
* '''template_index''': Which occurance of the template to look for the parameter in (default: 1)
* '''parameter_index''': Which occurance of the parameter to look for (default: 1; only applies when <code>ignore_subtemplates</code> is false)
* '''ignore_subtemplates''': If parameters should only be searched for in the top-level template, ignoring the parameters in subtemplates (default: false)
* '''only_subtemplates''': If parameters should only be searched for in subtemplates of the top-level template (default: false)
* '''ignore_blank''': Whether or not blank values should count towards <code>parameter_index</code> (default: false)
* '''treat_as_regex''': Whether or not the template string(s) should be treated as a lua regex (default: false)

=== getTemplate ===
getTemplate takes 3 arguments: The page name (string), the template/s (string or table of strings), and an optional options table. It will return either <code>true</code> and the text of the requested template or <code>false</code> and a reason for failure.

getTemplate supports the options <code>template_index</code> and <code>treat_as_regex</code> options from getParameter.

=== Helper functions ===
The module exposes some of the helper functions used (<code>matchAllTemplates</code>, <code>getParameters</code>, and <code>getAllParameters</code>) for convenience. Each function has some comments above it in the code explaining its rough purpose.

== Template functions ==
<code>main</code> implements {{tl|Template parameter value}} and acts as a template wrapper for <code>getParameter</code>.

<code>hasTemplate</code> implements {{tl|HasTemplate}} and somewhat acts as a wrapper for <code>getTemplate</code> (it only provides if the template was found, not the template text itself).


== Testcases ==
== Testcases ==

Revision as of 23:56, 23 February 2024

Implements {{Template parameter value}} and {{HasTemplate}}, and can be used from other modules.

Module functions

getParameter

getParameter takes 4 arguments: The page name (string), the template/s (string or table of strings), the parameter (string), and an optional options table. It will return either true and the contents of the requested parameter or false and a reason for failure.

The following options are available:

  • template_index: Which occurance of the template to look for the parameter in (default: 1)
  • parameter_index: Which occurance of the parameter to look for (default: 1; only applies when ignore_subtemplates is false)
  • ignore_subtemplates: If parameters should only be searched for in the top-level template, ignoring the parameters in subtemplates (default: false)
  • only_subtemplates: If parameters should only be searched for in subtemplates of the top-level template (default: false)
  • ignore_blank: Whether or not blank values should count towards parameter_index (default: false)
  • treat_as_regex: Whether or not the template string(s) should be treated as a lua regex (default: false)

getTemplate

getTemplate takes 3 arguments: The page name (string), the template/s (string or table of strings), and an optional options table. It will return either true and the text of the requested template or false and a reason for failure.

getTemplate supports the options template_index and treat_as_regex options from getParameter.

Helper functions

The module exposes some of the helper functions used (matchAllTemplates, getParameters, and getAllParameters) for convenience. Each function has some comments above it in the code explaining its rough purpose.

Template functions

main implements {{Template parameter value}} and acts as a template wrapper for getParameter.

hasTemplate implements {{HasTemplate}} and somewhat acts as a wrapper for getTemplate (it only provides if the template was found, not the template text itself).

Testcases

Testcases are available at Module talk:Template parameter value/testcases