![]() | This is the module sandbox page for Module:If preview (diff). |
![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This Lua module is used on approximately 350,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 uses TemplateStyles: |
This module implements {{If preview}} and {{Preview warning}}. It helps templates/modules determine if they are being previewed.
Prefer implementing the template versions in other templates.
In a module to use the main()
, you need to pass a frame table with an args table.
For the preview warning, use _warning()
.
local p = {}
--[[
is_preview
This function returns a boolean indicating whether the page is a preview.
]]
local function is_preview()
local revision_id = mw.getCurrentFrame():preprocess('{{REVISIONID}}')
-- {{REVISIONID}} is usually the empty string when previewed.
return not revision_id == nil and not revision_id == ''
end
--[[
main
This function returns either the first argument or second argument passed to
this module, depending on whether the page is being previewed.
Usage:
{{#invoke:If preview|main|value_if_preview|value_if_not_preview}}
]]
function p.main(frame)
if is_preview() then
return frame.args[1] or ''
else
return frame.args[2] or ''
end
end
--[[
preview_warning
This function returns a "preview warning", which is the first argument marked
up with HTML and some supporting text, depending on whether the page is being previewed.
Usage:
{{#invoke:If preview|warning|preview_warning_text}}
]]
function p.warning(frame)
if not is_preview() then return '' end
local warning = frame.args[1]:match('^%s*(.-)%s*$') or ''
if warning == '' then
warning = 'Something is wrong with this template'
end
return mw.ustring.format(
'%s<div class="preview-warning"><strong>Preview warning:</strong> %s</div>',
frame:extensionTag{
name = 'templatestyles', args = { src = 'Module:Preview warning/styles.css' }
},
warning
)
end
return p