Helper module to facilitate a substituted template transform into a template transclusion.

Maintenance templates, such as {{Citation needed}} or {{Refimprove}}, should never be substituted. A trick to avoid that is to make a template substitute to its transcluded form.

Usage

To turn a template into a self-substituting template, wrap the existing template code with:

  1. {{ {{{|safesubst:}}}#invoke:Unsubst||$B=
  2.  
  3. [ ... existing template code ... ]
  4.  
  5. }}

The wikitext to display when not substed must be given as "$B". A parameter "$N" may also be seen in some templates; this was required in an older version of the module, but is no longer necessary and may be removed. Such templates are automatically placed in Category:Calls to Module:Unsubst that use $N.

All other parameters passed to the #invoke will be copied to the generated template invocation as default values. If the value of any of these default parameters is "__DATE__", that value in the generated template invocation will be the current month and year.

Some templates have a <noinclude> but no matching </noinclude> at the end of the template. In such cases the missing </noinclude> must be added before the ending }}.

Example

Consider a template Template:Example containing the following code:

  1. {{ {{{|safesubst:}}}#invoke:Unsubst||foo=bar |date=__DATE__ |$B=
  2.  
  3. [ ... Template code goes here ... ]
  4.  
  5. }}
Original Result
{{subst:example}} {{Example|foo=bar|date=February 2025}}
{{subst:example|foo=X}} {{Example|foo=X|date=February 2025}}
{{subst:example|baz=X}} {{Example|foo=bar|baz=X|date=February 2025}}
{{subst:example|date=January 2001}} {{Example|foo=bar|date=January 2001}}

  1. local p = {}
  2. local specialParams = {
  3. ['$N'] = 'template name', -- Deprecated, but keeping until it is removed from transcluding templates
  4. ['$B'] = 'template content',
  5. }
  6. p[''] = function ( frame )
  7. if not frame:getParent() then
  8. error( '{{#invoke:Unsubst|}} makes no sense without a parent frame' )
  9. end
  10. if not frame.args['$B'] then
  11. error( '{{#invoke:Unsubst|}} requires parameter $B (template content)' )
  12. end
  13. if mw.isSubsting() then
  14. ---- substing
  15. -- Combine passed args with passed defaults
  16. local args = {}
  17. for k, v in pairs( frame.args ) do
  18. if not specialParams[k] then
  19. if v == '__DATE__' then
  20. v = mw.getContentLanguage():formatDate( 'F Y' )
  21. end
  22. args[k] = v
  23. end
  24. end
  25. for k, v in pairs( frame:getParent().args ) do
  26. args[k] = v
  27. end
  28. -- Build an equivalent template invocation
  29. -- First, find the title to use
  30. local titleobj = mw.title.new(frame:getParent():getTitle())
  31. local title
  32. if titleobj.namespace == 10 then -- NS_TEMPLATE
  33. title = titleobj.text
  34. elseif titleobj.namespace == 0 then -- NS_MAIN
  35. title = ':' .. titleobj.text
  36. else
  37. title = titleobj.prefixedText
  38. end
  39. -- Build the invocation body with numbered args first, then named
  40. local ret = '{{' .. title
  41. for k, v in ipairs( args ) do
  42. if string.find( v, '=', 1, true ) then
  43. -- likely something like 1=foo=bar, we need to do it as a named arg
  44. break
  45. end
  46. ret = ret .. '|' .. v
  47. args[k] = nil
  48. end
  49. for k, v in pairs( args ) do
  50. ret = ret .. '|' .. k .. '=' .. v
  51. end
  52. return ret .. '}}'
  53. else
  54. ---- Not substing
  55. -- Just return the "body"
  56. return frame.args['$B'] .. (frame.args['$N'] and frame:getParent():getTitle() == mw.title.getCurrentTitle().prefixedText and '[[Category:Calls to Module:Unsubst that use $N]]' or '')
  57. end
  58. end
  59. return p