What is cargo_attach?[]
The parser function {{#cargo_attach:}}
is used to tell Cargo what pages write rows to certain tables. The parser function {{#cargo_declare:}}
has a built-in {{#cargo_attach:}}
, so if your template declares a table, there's no need to also attach it to that same template. At most one table can be attached to any single template.
Why use cargo_attach?[]
When you recreate a Cargo table, Cargo initiates a set of jobs on the server to discover and then update every page that writes rows to that table. The discovery works using WhatLinksHere and the two parser functions {{#cargo_declare:}}
and {{#cargo_attach:}}
- each template that claims to write rows is discovered by finding usages of these two parser functions, and then each page that transcludes those templates is then updated by this process.
There's a few reasons you might need to use {{#cargo_attach:}}
specifically, instead of just {{#cargo_declare:}}
:
- Since a template can only declare one table, if your template stores to more than one table, you'll have to use
{{#cargo_attach:}}
for the others. - If two templates store to the same table, only one of those templates can have the declaration, so you'll need to attach the table to the other template.
- Maybe for stylistic / documentation reasons, you want to declare the table in its own template (Leaguepedia does this).
- Occasionally Cargo randomly bugs and gives you random database errors when you try to declare a template there; a workaround is to move the declaration to another location, and then use an attach from the original template.
Attaching multiple tables to the same template[]
This is not possible to do, but there is a workaround.
A template can attach at most one table and can declare at most one table. Say {{Cats}}
needs to attach both [Cats]
and [Kittens]
(this is a fake example, those tables don't actually exist on this wiki).
So {{Cats}}
attaches [Cats]
the normal way, and then we make a special template called {{Kittens/CargoAttach}}
(the name of this template can be anything, but this is the convention used by Leaguepedia). The code of {{Kittens/CargoAttach}}
is the following:
<includeonly></includeonly><noinclude>{{#cargo_attach:_table=Kittens}}</noinclude>
Now at {{Cats}}
we do the following:
<includeonly>Source code about cats More source code even more source code End of source code<!-- -->{{Kittens/CargoAttach}}</includeonly><noinclude>{{#cargo_attach:Cats}}</noinclude>
Here {{Kittens/CargoAttach}}
is actually transcluded into every pages that uses {{Cats}}
- and that's the point. Each page that uses {{Cats}}
needs to transclude some template that attaches [Kittens]
- since {{Cats}}
itself can't do that, we use {{Kittens/CargoAttach}}
to accomplish this instead.