Help:Transclusion

Jump to navigation Jump to search

Transclusion is generally the inclusion of the content of a document into another document by reference. In a wiki context, it is the use of the template functionality of MediaWiki to include the same content in multiple documents without having to edit those documents separately.

How transclusion works

</translate>

<translate> To transclude any source page (within a single MediaWiki project, such as Wikipedia) within another target page, include the following code: {{SOMEPAGE}}

Whenever the target page A is rendered, the entire content of the source page B (in this case, SOMEPAGE), will be rendered at the location where the transclusion tag was placed.

For example, you might decide to place a welcome message on every newcomer's talk page. Transclusion creates a "live" link between the template-page and the target-page(s) upon which the message should appear. When the template is edited, all the target-pages are edited too.

You might also decide to create a (template) page with your mailing address and include that template on not only your page, but all your friends' pages as well. When you move your television and couch to another apartment, you can change your address template, and that new information will automatically update on all your friends' pages.

Transclusion Markup & Syntax

</translate> <translate> If the source is in the Template namespace (e.g. "[[<tvar name=1>Template:Welcome</tvar>]]"), just use the name itself, alone:</translate> {{Welcome}}

<translate> If the source is in the Main article namespace (e.g. "[[<tvar name=1>VisualEditor</tvar>]]"), a colon (<tvar name=2>:</tvar>) must be added in front of the name:</translate> {{:VisualEditor}}

<translate> If the source is in any other namespace (e.g. "[[<tvar name=1>User:Example</tvar>]]"), you must use the full name, including the namespace:</translate> {{User:Example}}

<translate> If the source is a [[<tvar name=1>Special:MyLanguage/Help:Subpages</tvar>|subpage]] of the target page, (e.g. "[[<tvar name=2>Transclusion/ja</tvar>]]"), you can simply specify the name of the subpage regardless of namespace:</translate> {{/ja}}

<translate>

Partial transclusion

By using "<tvar name=noincl>noinclude</tvar>", "<tvar name=onlyincl>onlyinclude</tvar>" and "<tvar name=inclonly>includeonly</tvar>" markup, it is possible to transclude part of a page rather than all of it. Such partial transclusions can also be achieved by transcluding from other pages such as subpages. It is often desirable not to transclude some information, such as template documentation and categories.

Examples of when to use full versus partial transclusion

</translate> <translate> Use full transclusion when you want to include the full content of a source page in a target page.</translate> <translate> For example, you could include the full content of a page about apples in a page about tree fruits.</translate> <translate> Use partial transclusion when you only want to include part of a source page in a target page.</translate> <translate> For example, you could include only information about Granny Smith apples from a page about apples in a page about ingredients commonly used in baking.</translate>

<translate>

Partial transclusion markup

</translate>

  • noinclude - <translate> The markup <tvar name=1><noinclude>...</noinclude></tvar> means that the text between the tags is visible exclusively on the source page and cannot be transcluded onto another page.</translate> <translate> This is useful for template documentation and categories.</translate>
  • includeonly - <translate> The markup <tvar name=1><includeonly>...</includeonly></tvar> means that the text between the tags will be hidden on the source page and visible only when transcluded onto a different page.</translate> <translate> This can be useful, for example, for adding categories to pages transcluding a template, without adding the template itself to these categories.</translate>
  • onlyinclude - <translate> The markup <tvar name=1><onlyinclude>...</onlyinclude></tvar> means that the text between the tags will be visible on the source page and visible when transcluded onto a different page.</translate> <translate> This is the most subtle of the partial transclusion tags because it often overrules the others.</translate> <translate> If there is at least one pair of "<tvar name=1>onlyinclude</tvar>" tags on a page, then whenever this page is transcluded, it is only the material within the "<tvar name=2>onlyinclude</tvar>" tags that is transcluded.</translate> <translate> There can be several such "<tvar name=1>onlyinclude</tvar>" sections on a page.</translate> <translate> This can be useful, for example, to repeat a small part of one page on a second one: just surround the small part by <tvar name=1>onlyinclude</tvar> tags, and transclude it onto the second page.</translate>


<translate> Markup</translate> <translate> Output when transcluded</translate>
<nowiki><noinclude> My content </noinclude></nowiki> <noinclude> My content </noinclude>
<nowiki><includeonly> My content </includeonly></nowiki> <includeonly> My content </includeonly>
<nowiki><onlyinclude>My content</onlyinclude></nowiki> My content

<translate> You can nest all three transclusion types within each other, which enables you to refine exactly what content is displayed on the current page and is visible on transcluded pages.

Subpages

You can cut and paste the text to be transcluded into a subpage, then use the name of the subpage in the transclusion template. This approach can be used only where subpages are enabled; for example, on the English Wikipedia, subpages can be used only from User, Talk or Wikipedia pages; currently, subpages cannot be created from main article pages.

Example: you want to discuss the deletion and redirecting of Pussycat to Cat. First, create the subpage Talk:Pussycat/Let's delete Pussycat!, write your comment into it, then transclude it in Talk:Pussycat and Talk:Cat using the template {{Talk:Pussycat/Let's delete Pussycat!}}. Comments posted in either talk page will be shown in both.

Special pages

Some pages on <tvar name=1>Special:Specialpages</tvar> can be transcluded, e.g.: </translate>

<translate> Sample: <tvar name=1>{{Special:Newpages/3}}</tvar> gives 3 new pages.

[[<tvar name=manparamindex>Special:MyLanguage/Manual:Parameters to index.php</tvar>|URL parameters]] can be given like template parameters, e. g. </translate>{{Special:RecentChanges|namespace=10|limit=5}}.

<translate>

Alternatives to transclusion by template

Occasionally you may want to use a template, but for one reason or another may not want to use transclusion. The automatic one-time copying of a template's code/text to the location of a template tag is called Template substitution or subclusion (substitution + transclusion). To subclude a template's code/text, the template tag is modified from the standard transclusion tag to a substitution tag, simply by adding subst:, creating a tag with the form: {{subst:template name}}.

Labeled Section Transclusion

An extension called [[<tvar name=extlst>Special:MyLanguage/Extension:Labeled Section Transclusion</tvar>|Labeled Section Transclusion]] enables marked sections of text to be transcluded.

<translate>

Semantic MediaWiki

The [[<tvar name=extsmw>Special:MyLanguage/Extension:Semantic MediaWiki</tvar>|Semantic MediaWiki extension]] uses inline queries to query and display semantic data. There are currently two ways to perform transclusion or something similar:

  1. Transclusion proper. The display format "embedded" ensures that selected pages (mainspace articles, templates, etc.) are transcluded. As with templates, sections placed between noinclude tags will be omitted.
  2. Selected passages in a page can be assigned a property of type 'Text'. These selections can be requested by running an inline query for this type of property.