Help:Random choice

From RationalWiki
Jump to navigation Jump to search

New style (parserfunction)[edit]

To choose between one or more options, use the choose parserfunction. The format is similar to using templates:

{{#choose:foo|bar|baz}}

Note that the first option does not have a | (pipe) character before it, all others do.

If you'd like some options to be shown more often than others, you can add a weight before the option, like so:

{{#choose:
2=This option will be shown twice as often as a normal option.
|By default, options have a weight of 1.
|1.5=This option also has a weight of 1.  Fractional portions are ignored.
}}

Warning: If an option contains an = sign, you can't omit the weight, so for example

{{#choose:option 1 has no equal sign|option two has an = sign}} 

is invalid (the weight of option two will be 0); instead just specify the weight explicitly:

{{#choose:option 1 has no equal sign|1=option two has an = sign}}

Substitution[edit]

Like templates, parser functions can also be substituted. {{subst:#choose:foo|bar|baz}} will select one option when you save a page and will replace the choose function with the text of that option. However, substitution is not recursive, so if a template using choose is substituted, the entire choose function with all the options will be copied into the page text, which is probably not what you want.

The simple solution is to use subst:expand: instead of subst:, e.g.:

{{subst:expand:Assquote}}

will take Template:Assquote, recursively substitute every template and parser function in it, and then copy the resulting text into the page when it is saved:


"Anonymous User" (sounds like another self-centered liberal, like Obama), you have free will and can laugh at anything that you can persuade yourself to be funny. I skimmed through your recent edits and there is an anti-Christian tone to your postings, so admit it, you're a supporter of infanticide. Furthermore, you are in denial that preaching about Hell prevents abortion. Got news for you: the Bible is the most logical book ever written. Lots of people are learning that, surprise, surprise, the law applies to vandalism on the internet too. Come back when you can learn out to spell "superior".--Aschlafly 18:26, 15 June 2017 (UTC)


Sometimes you don't want every template substituted. In this case, instead of using subst:expand:, you should change subst: to safesubst: and add includeonly around the safesubst: in the template's choose function, and then use normal substitution:

{{<includeonly>safesubst:</includeonly>#choose:foo|bar|baz|}}

Unlike transcluded random templates, which change with every reload of the page, substituted templates will remain the same. Transcluded templates increase page loading times, and if they are used on a lot of pages, editing them will slow down the entire site for a long time.

Old style (tag)[edit]

Using the parser function is preferable, but you can also use the <option> tag, like so:

<choose>
<option>This is the first choice.</option>
<option>This is the second choice.</option>
<option>And so on...</option>
</choose>

From each set of <choose> tags, precisely one <option> tag will be selected at random, and its contents shown.

If you'd like some options to be shown more often than others, you can add weight to the option tags, like so:

<choose>
<option weight="2">This option will be shown twice as often as a normal option.</option>
<option>By default, options have a weight of 1.</option>
<option weight="1.5">This option also has a weight of 1.  Fractional portions are ignored.</option>
</choose>

Anything can be placed inside of option tags: links, images, even templates. However, template parameters will not be expanded, so they cannot be used in the option text or as a weight. Magic words (such as {{CURRENTDAY}}) and parser functions (such as {{#if:...}}) cannot be used as a weight. The option tag cannot be substituted either. In those cases, you can use the parser function version.

See also[edit]