Module:Userbox
![]() | This Lua module is used on approximately 330,000 pages, or roughly 1% of all pages. To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them. |
![]() | This module depends on the following other modules: |
This module does the processing for three userbox templates, {{userbox}}, {{userbox-2}} and {{userbox-r}}.
Template | Description | Examples |
---|---|---|
{{userbox}} | Makes userboxes with an id on the left-hand side, or with no id. | Lua error in package.lua at line 80: module 'Module:HtmlBuilder' not found.
Lua error in package.lua at line 80: module 'Module:HtmlBuilder' not found. |
{{userbox-2}} | Makes userboxes with an id on both the left- and right-hand sides. | Lua error in package.lua at line 80: module 'Module:HtmlBuilder' not found. |
{{userbox-r}} | Makes userboxes with an id on the right-hand side. | Lua error in package.lua at line 80: module 'Module:HtmlBuilder' not found. |
To use any of these templates from a wiki page, please see the individual template pages for documentation. To generate userboxes directly from Lua, read on.
Generating userboxes from Lua
To generate a userbox directly from Lua, first load the module.
local userbox = require('Module:Userbox')
You can then run any of the three templates with the code:
userbox.main(functionName, args)
For {{userbox}} use the function name "_userbox
"; for {{userbox-2}} use the function name "_userbox-2
"; and for {{userbox-r}} use the function name "_userbox-r
". The args
parameter is a table of arguments to pass to the different userbox functions. To see a list of valid arguments, please consult the individual template pages.
Tracking categories
-- This module implements {{userbox}}.
local getArgs = require('Module:Arguments').getArgs
local htmlBuilder = require('Module:HtmlBuilder')
local catHander = require('Module:Category handler')
local p = {}
function p.main(frame)
local args = getArgs(frame)
return p._main(args)
end
function p._main(args)
local userbox = p.userbox(args)
local cats = p.categories(args)
return userbox .. (cats or '')
end
function p.userbox(args)
-- Calculate border width.
local borderWidth = args['border-width'] or args['border-s']
borderWidth = tonumber(borderWidth)
if not borderWidth or borderWidth < 0 then
borderWidth = 1
end
-- Get border color.
local borderColor = args['border-color'] or args[1] or args['border-c'] or args['id-c'] or '#999'
-- Get background color.
local backgroundColor = args['info-background'] or args[2] or args['info-c'] or '#EEE'
-- Calculate width.
local width = 240 - 2 * borderWidth
-- Build the box.
local root = htmlBuilder.create('div')
root
.css('float', args.float or 'left')
.css('border', tostring(borderWidth) .. 'px solid ' .. borderColor)
.css('margin', '1px')
.css('width', tostring(width) .. 'px')
.addClass('wikipediauserbox')
.addClass(args.bodyclass)
.tag('table')
.css('border-collapse', 'collapse')
.css('width', width)
.css('margin-bottom', '0')
.css('background', backgroundColor)
return tostring(root)
end
function p.categories(args)
end
return p