Help:Creating a Template
These instructions are for members of the Templates Project. First see See Template Guidelines.
Templates are a very useful feature of wikis. They allow you to take content common to many pages and easily include it on each page without copying it in full.
For example, Template:Mayflower Passenger is used on all profiles of Mayflower passengers. It displays a picture of the Mayflower, and guides the reader to the Mayflower Project. All this can be displayed on a profile with just the text {{Mayflower Passenger}}.
Caution: Do not experiment on existing templates. Any changes made to a template will appear on all the pages that call them, so your change may affect hundreds of profiles.
You must be a member of the Templates Project to create and edit templates.
Contents |
Creating a Basic Template
A simple template that displays exactly the same everywhere it's used is very easy to make.
- Go to the template page. This will be https://www.wikitree.com/wiki/Template: followed by the name of your template (eg. https://www.wikitree.com/wiki/Template:Mayflower_Passenger ).
- Click the [edit] link in the top right corner.
- Type whatever you want to appear wherever you use the template. This can include wiki markup and/or HTML.
- Save the page.
Now, wherever you put the name of your template in double braces (eg. {{Mayflower Passenger}}), whatever you put in the template will appear.
Creating a Template with Variables
Some templates are more complicated; they have a common appearance, but differ based on specifics entered when the template is called. An example is Template:Migrating Ancestor. It always looks more or less the same, but the place names and flag images change according to what fits the profile. That template is used on a profile using code like this:
- {{Migrating Ancestor
- | origin = France
- | destination = Canada
- | origin-flag = Flags.png
- | destination-flag = Flags-1.png
- }}
Creating templates with variables is similar to creating a basic template. The only difference is that wherever you want a variable, you put the name of the variable in triple braces. For example, in the Migrating Ancestor template, {{{origin}}} is put in the place where the origin is named. It acts as a placeholder for whatever comes after origin= when the template is called.
WikiTree Variables in Templates
The following variables for WikiTree database fields can be included in a template:
- Prefix: {{#profile:Prefix}}
- Preferred First Name: {{#profile:RealName}}
- MiddleInitial: {{#profile:MiddleInitial}}
- Last Name at Birth: {{#profile:LastNameAtBirth}}
- Current Last Name: {{#profile:LastNameCurrent}}
- Suffix: {{#profile:Suffix}}
- BirthDateDecade: {{#profile:BirthDateDecade}}
- DeathDateDecade: {{#profile:DeathDateDecade}}
This enables you to say something like "Joe speaks French" instead of "This user speaks French".
When you want a full name, you can use Template:Name, i.e. {{Name}} as a template-within-a-template (see below.) It combines the following names fields in one string. There is a conditional so that the Last Name at Birth only appears if it's different from the Current Last Name.
- {{#profile:RealName}} ({{#profile:LastNameAtBirth}}) {{#profile:LastNameCurrent}} {{#profile:Suffix}}
Using a Template Within a Template
This is where things can get a bit complicated. Within a template, another template can be called. All of the project boxes are examples of this. They all call Template:Project Box. The reason for this is that the project boxes are all similar. So if we change how one of them looks, we want them all to change. That is made possible by the fact that they all build on a common template, which, when changed, would change all of them.
For instructions on creating a new project box, see Template:Project Box.
Images for Templates
Images used in templates are usually uploaded to a project page when the template is created for specific project, or to a page for a collection of related images (such as Flags). Images that are intended for use in templates that do not belong elsewhere may be uploaded to Space:Template Image.
Basic Template Writing Tutorial
I use a personal "sandbox" template to test a template idea before creating the final template. Pick a unique name that makes it obvious it's your sandbox. I picked RolandDev for mine. For the rest of the tutorial, I will use HelloWorld, but you should replace that with the name of your personal template development sandbox.
Hello, World!
- Create your template page. In your browser address bar, type www.wikitree.com/wiki/Template:NameOfYourSandboxGoesHere. Pressing enter should take you to a page that starts with "There is currently no text in this page...". (If you get something else, you might have chosen a name that is already in use, try again with a different name.) Click the edit link to start editing your template.
- In the edit box, type "Hello, World!" (without the quotes). Save that page.
- In a wiki page, such as your profile or a Space: page you might use to test wiki stuff, try out the template by enclosing its name in a double pair of braces.
In the edit page:
- {{HelloWorld}}
Result:
Hello, World!
- Congratulations, you wrote your first template!
Being more specific
The ability to specify extra information when using a template is what makes them powerful. Such extra information is specified when using a template by adding a pipe, "|", after the template name, followed by that information, typically called an argument or a parameter.
- Modify your template to accept a parameter. Edit your template page, changing "World" to "{{{1}}}". The triple braces indicate where the parameter should be inserted, and the 1 specifies that the first parameter goes there. Also modify the page where you are using the template and add |Bob after the template name, but inside the double braces.
Template Source:
- Hello, {{{1}}}!
Template use:
- {{HelloWorld|Bob}}
Result:
Hello, Bob!
- What happens if you don't specify a parameter? Try removing the parameter where you use the template.
Template Use:
- {{HelloWorld}}
Result:
Hello, {{{1}}}!
This is probably not a template we would want to use without specifying a parameter. In this case, this is a required parameter. We can make a parameter optional by specifying a default value.
- Edit the template source to add "World" as the default when a parameter is not specified.
Template Source:
- Hello, {{{1|World}}}!
Template Use:
- {{HelloWorld}}
- {{HelloWorld|Bob}}
Result:
Hello, World!
Hello, Bob!
Controlling WHAT appears
When Page A is called as a template (i.e. using braces {{ }}) from Page B the system displays the content of Page A in the spot on Page B where the call is made - this is one form of inclusion known as transclusion (the other form of inclusion is substitution.) By default the entire content of Page A will be transcluded, however this is seldom desirable. To control what part of Page A appears on Page B you can use 'inclusion tags' on Page A to mark the content that should be included.
There are three inclusion tags available:
- <onlyinclude> which means show this content on this page and ONLY show this content on other pages.
- <includeonly> which means show this content on other pages but NOT on this page.
- <noinclude> which means show this content on this page but NOT on other pages.
Attention must be drawn to the statement that <onlyinclude> tells the page to ONLY show this marked content, an important difference between this tag and the <includeonly> tag. With the <includeonly> tag any content outside of the tag will also be transcluded unless it is marked with <noinclude> - attention must be paid to this as it can introduce unintended white-space and line breaks in the displayed content.
While using <onlyinclude> can be simpler, because it does not require separate marking of content with <noinclude> and ensures anything outside the tags will not be included, any dynamic content (i.e. anything based on parameters) will be displayed blank, or with default text, which does not represent how the transcluded content should appear when used, and can therefore be confusing. It is therefore usually advisable to use the <includeonly> and <noinclude> tags.
This page was last modified 15:07, 15 October 2021. This page has been accessed 4,779 times.