no image

Templates Lesson 1: Controlling Template Inclusion

Privacy Level: Public (Green)
Date: [unknown] [unknown]
Location: [unknown]
Profile manager: Steven Harris private message [send private message]
This page has been accessed 248 times.

Template Lessons > Lesson 1

Scope

When designing templates, you can control which parts of a template will be seen by the use of the inclusion tags:

  1. noinclude - Any text or code that is added between the <noinclude> and </noinclude> tags will be seen only when the template's page is being viewed directly, but not when it is included in another page. This is useful when you want to include text or code in a template that you do not want to propagate to any pages which include it, such as:
    • Category links when you are only wanting to categorize the template page itself;
    • Explanatory text about how to use the template
  2. includeonly - Any text or code that is added between the <includeonly> and </includeonly> tags will be processed and displayed only when the page is being included to another page, but not when the template page is being viewed directly. This is useful when you do not want to expose or execute the underlying template code on the template page itself.

Using Inclusion Tags

Example 1 - By default, a template's content is displayed in its entirety, both when viewed directly and when included in another page. If we do not use any inclusion tags:

Welcome to WikiTree!
To insert the text "Welcome to WikiTree" on a page, use the code: <nowiki>{{Test}}</nowiki>

When a user visits the Template:Test page directly, they will see:

Welcome to WikiTree!
To insert the text "Welcome to WikiTree" on a page, use the code: {{Test}}

And if the template is called from another page with {{Test}}, they will see:

Welcome to WikiTree!
To insert the text "Welcome to WikiTree" on a page, use the code: {{Test}}

The absence of inclusion tags tells the system that all information should be shown, no matter the placement on the page.


Example 2 - If we use the noinclude and includeonly tags on Template:Test:

<includeonly>Welcome to WikiTree!</includeonly>
<noinclude>
To insert the text "Welcome to WikiTree" on a page, use the code: <nowiki>{{Test}}</nowiki></noinclude>

When a user visits the Template:Test page directly, they will only see the text contained within noinclude:

To insert the text "Welcome to WikiTree" on a page, use the code: {{Test}}

And if the template is called from another page with {{Test}}, only the template code within includeonly will display:

Welcome to WikiTree!

Learning Exercise

If we use a mixture of text, both inside and outside of inclusion tags, what would the result be? Use your Sandboxes to investigate the following scenario:

[[Category:Test]]
<includeonly>Welcome to WikiTree!</includeonly>
<noinclude>
To insert the text "Welcome to WikiTree" on a page, use the code: {{Test}}</noinclude>

What are your results on the Template page:

  • ... for the text within inclusion tags?
  • ... for the text outside of inclusion tags?

What are your results when calling your template:

  • ... for the text within inclusion tags?
  • ... for the text outside of inclusion tags?

Continue to ⟶ Templates Lesson 2: Organizing Templates





Collaboration


Comments

Leave a message for others who see this profile.
There are no comments yet.
Login to post a comment.