Search code examples
mediawikimediawiki-templates

How can I substitute a magic word only when transcluding template without using #invoke?


I'm trying to make a template that shows the time it was placed. I tried this:

This is a template ({{<includeonly>subst:</includeonly>CURRENTTIMESTAMP}})

but when transcluded it would show up as This is a template ({{subst:CURRENTTIMESTAMP}}) instead of substituting the timestamp. I then tried

This is a template ({{{{{|safesubst:}}}CURRENTTIMESTAMP}})

but that didn't substitute it; the timestamp would keep updating. Next, I tried

This is a template ({{<includeonly>{{{|safesubst:}}}</includeonly>CURRENTTIMESTAMP}})

but no dice.

I know it's possible with {{#invoke:}} but I don't have that option. Could someone help me out?


Solution

  • Consider how templates are stored. If you recursively subst the template, the contents of the template are stored in the wikitext of the including page, with CURRENTTIMESTAMP replaced with the actual timestamp. If you do not subst the template, the wikitext will only contain {{MyTemplate}}, and the timestamp of the edit is not stored anywhere; when the the parser renders the article and encounters CURRENTTIMESTAMP, it will just use the current timestamp. There is no way around that, #invoke or not.

    What you can do is make the timestamp into a parameter, so in the including page you would write {{MyTemplate|{{subst:CURRENTTIMESTAMP}}}} and then the timestamp gets stored in the page. You can create a wrapper template with that exact content so that users only have to type {{subst:MyTemplateWrapper}} which is a bit easier. You can even make the template show an error / add an error category when used without substing, although that's a bit more involved.