Module:Transclude
Appearance
From Meta, a Wikimedia project coordination wiki
warning Warning: Do not edit. This page is maintained by an automated tool. All edits should be done at commons.wikimedia.org. (translate this warning)
This module is rated as beta, and is ready for widespread use. It is still new and should be used with some caution to ensure the results are as expected.
Usage
{{#invoke:Transclude|template_name}}
The above documentation is transcluded from Module:Transclude/doc. (edit | history)
Editors can experiment in this module’s sandbox (create | mirror) and testcases (create) pages.
Please add categories to the /doc subpage. Subpages of this module.
Editors can experiment in this module’s sandbox (create | mirror) and testcases (create) pages.
Please add categories to the /doc subpage. Subpages of this module.
-- <nowiki> -------------------------------------------------------------------------------- -- Module that allows transcluding a template with all parent arguments. -- Useful in `/i18n` template subpages that delegate to a `/layout` subpage. -- -- @module transclude -- @author [[User:ExE Boss]] -------------------------------------------------------------------------------- require('strict') localcheckType=require('libraryUtil').checkType; localp={} localINVALID_TITLES={ ["."]=true, [".."]=true, }; localfunction_ne(value) returnvalueandvalue~='' end -------------------------------------------------------------------------------- -- Helper function that merges argument tables. -- -- @function mergeArgs -- @param {table} inArgs -- @param {table} outArgs -------------------------------------------------------------------------------- localfunctionmergeArgs(inArgs,outArgs,opts) checkType("mergeArgs",1,inArgs,"table"); checkType("mergeArgs",2,outArgs,"table"); checkType("mergeArgs",3,opts,"table",true); opts=optsor{} localignoredParams=opts.ignoredParams; localignoredPrefix=opts.ignoredPrefix; forname,valueinpairs(inArgs)do if(type(name)=="string")then ifnot( (ignoredParamsandignoredParams[name]==true) or(ignoredPrefixand ignoredPrefix==name:sub(1,ignoredPrefix:len())) )then outArgs[name]=value; end else outArgs[name]=value; end end returnoutArgs; end -- Exported for testing: p['#mergeArgs']=mergeArgs; localmt={}; functionmt.__index(_,name) if( type(name)~="string" orINVALID_TITLES[name] ormw.ustring.find(name,"#",nil,true) )then returnnil; end localtitle=mw.title.new(name,"Template") if(nottitle)then returnnil; end returnfunction(frame) checkType('transclude["'..name..'"]',1,frame,"table"); localparentFrame=frame:getParent(); localignoredParams,ignoredPrefix; if_ne(frame.args["#ignoredParams"])then ignoredParams={}; forminmw.text.gsplit(frame.args["#ignoredParams"],'|',true)do ignoredParams[mw.text.trim(m)]=true; end end if_ne(frame.args['#ignoredPrefix'])then ignoredPrefix=frame.args['#ignoredPrefix']; end localargs={}; if(parentFrame)then mergeArgs(parentFrame.args,args,{ ignoredParams=ignoredParams, ignoredPrefix=ignoredPrefix, }); end mergeArgs(frame.args,args,{ ignoredPrefix="#", }); if(nottitle.exists)then return"[[:"..title.fullText.."]]"; end returnframe:expandTemplate{ title=title, args=args, }; end end returnsetmetatable(p,mt);